Use jquery's scroll bar to scroll and fix the div

Source: Internet
Author: User

Js & jquery

<Script src = "jquery-1.4.1.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
Var loaded = true;
Var top = $ ("# demo"). offset (). top;
Function Add_Data ()
{
Var scrolla = $ (window). scrollTop ();

// Alert (top)


Var cha = parseInt (top)-parseInt (scrolla );

Alert (top + "-" + scrolla + "=" + cha );


If (loaded & cha <= 0)
{
$ ("# Demo"). removeClass ("demo2"). addClass ("demo1 ");
$ ("# Demo" ).html ("I am blue now. I will display the scroll bar in red when I pull it up. I have fixed it. ");
Loaded = false;
}
If (! Loaded & cha> 0)
{
$ ("# Demo"). removeClass ("demo1"). addClass ("demo2 ");
$ ("# Demo" ).html ("I am red now. Pull down the scroll bar and I will display it in blue. I am not fixed yet. ");
Loaded = true;
}
}
$ (Window). scroll (Add_Data );
});
</Script>

Style Sheet

<Style type = "text/css">
. Demo
{
Border: 1px solid # dcdcdc;
Width: 300px;
Margin-top: 10px;
Overflow: auto;
Text-align: left;
Background-color: # ffffff;
Height: 200px;
}
. Demo1
{
Position: fixed;
_ Position: absolute;
Top: 0px;
Background-color: # 0000ff;
Height: 200px;
Width: 300px;
Color: # ffffff;
}
. Demo2
{
Border: 1px solid # dcdcdc;
Width: 300px;
Margin-top: 10px;
Overflow: auto;
Text-align: left;
Background-color: # ff0000;
Height: 200px;
Color: # ffffff;
}
</Style>

Html

<Div class = "demo">
1
</Div>
<Div class = "demo">
2
</Div>
<Div class = "demo">
3
</Div>
<Div class = "demo">
4
</Div>
<Div class = "demo2" id = "demo">
I am red now. I will show the scroll bar down in blue. I am not fixed yet.
</Div>
<Div class = "demo">
6
</Div>
<Div class = "demo">
7
</Div>
<Div class = "demo">
8
</Div>
<Div class = "demo">
9
</Div>
<Div class = "demo">
10
</Div>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.