Home Web Front-end JS Tutorial Example code for div to be fixed at the top when scrolling to the top of the page_javascript skills

Example code for div to be fixed at the top when scrolling to the top of the page_javascript skills

May 16, 2016 pm 05:33 PM
scroll

This example uses Javascript to fix the DIV at the top when it reaches the top when scrolling the page. The effect is a bit flashy under IE, the effect URL: http://www.keleyi.com/keleyi/phtml/fixdiv.htm

The following is the code:

Copy code The code is as follows:

<body style="margin:0px;">
<div style="height:300px;background:#e0e0e0"></div>
<div id="fixedMenu_keleyi_com" style="background:#ffffff;width:100%;"><ul> <li><a

href="http://www.keleyi.com/menu/net/" target="_blank">.NET</a></li>
<li><a href="http://www.keleyi.com/menu/javascript/" target="_blank">Javascript</a></li>
<li> ;<a href="http://www.keleyi.com/menu/jquery/" target="_blank">jQuery</a></li>
<li><a href ="http://www.keleyi.com/menu/csharp/" target="_blank">C#</a></li>
<li><a href="http:/ /www.keleyi.com/menu/other/" target="_blank">Other</a></li>
<li><a href="http://www.keleyi .com/" target="_blank">Homepage</a></li></ul>
</div>
<div style="height:2600px;background: #999"></div>
<script type="text/javascript" src="http://www.keleyi.com/keleyi/pmedia/jquery-1.9.1.min.js" ></script>
<script type="text/javascript">
$(function () {
var ie6 = document.all;
var dv = $(' #fixedMenu_keleyi_com'), st;
dv.attr('otop', dv.offset().top); //Storage the original distance from the top
$(window).scroll(function () {
st = Math.max(document.body.scrollTop || document.documentElement.scrollTop);
if (st > parseInt(dv.attr('otop'))) {
if (ie6 ) {//IE6 does not support the fixed attribute, so this effect can only be achieved by setting position to absolute and top
dv.css({ position: 'absolute', top: st });
}
else if (dv.css('position') != 'fixed') dv.css({ 'position': 'fixed', top: 0 });
} else if (dv.css('position' ) != 'static') dv.css({ 'position': 'static' });
});
});
</script>
</body>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement scrolling to a specified element position in JavaScript? How to implement scrolling to a specified element position in JavaScript? Oct 22, 2023 am 08:12 AM

How to implement scrolling to a specified element position in JavaScript?

Monitor iframe scrolling behavior Monitor iframe scrolling behavior Feb 18, 2024 pm 08:40 PM

Monitor iframe scrolling behavior

HTML, CSS, and jQuery: Make an auto-scrolling bulletin board HTML, CSS, and jQuery: Make an auto-scrolling bulletin board Oct 27, 2023 pm 06:31 PM

HTML, CSS, and jQuery: Make an auto-scrolling bulletin board

How to use JavaScript to achieve the infinite scroll effect of automatically loading when scrolling to the bottom of the page? How to use JavaScript to achieve the infinite scroll effect of automatically loading when scrolling to the bottom of the page? Oct 27, 2023 pm 06:30 PM

How to use JavaScript to achieve the infinite scroll effect of automatically loading when scrolling to the bottom of the page?

How to implement scrolling switching effect of images in JavaScript? How to implement scrolling switching effect of images in JavaScript? Oct 20, 2023 pm 05:51 PM

How to implement scrolling switching effect of images in JavaScript?

How to implement full screen scrolling effect in Vue How to implement full screen scrolling effect in Vue Nov 08, 2023 am 08:42 AM

How to implement full screen scrolling effect in Vue

How to achieve smooth scrolling to the bottom button with CSS How to achieve smooth scrolling to the bottom button with CSS Nov 21, 2023 pm 05:11 PM

How to achieve smooth scrolling to the bottom button with CSS

How to use JavaScript to achieve the highlight effect of scrolling the navigation menu to a specified position? How to use JavaScript to achieve the highlight effect of scrolling the navigation menu to a specified position? Oct 19, 2023 am 09:46 AM

How to use JavaScript to achieve the highlight effect of scrolling the navigation menu to a specified position?

See all articles