Jquery uses div to implement the scroll bar Effect

Source: Internet
Author: User

I checked some div effects to implement the scroll bar. I read a lot and most of them do not meet the requirements.

The requirements are as follows:

1. Fixed div size, but not fixed content size in div

2. When you move the mouse over the content, if the content is too large, a scroll bar appears and you can drag it to view it, while the content width remains unchanged. Otherwise, the scroll bar is not displayed.

 

If the div overflow-y: scroll is used, the content width decreases.

 

Let's talk about the code.

Import jquery-1.4.1.min.js in article

 

The js Code is as follows:

 

Js Code

<Script type = "text/javascript" src = "js/jquery-1.4.1.min.js"> </script>

<Script type = "text/javascript">

Var check = false; // whether it is movable

$ (Document). ready (function (){

Var h = $ ("# scrollContent"). height ();

Var ih = $ ("# divcontent"). height ();

Var mh = parseInt (h * h/ih );

Var h1 = parseInt ($ ("# ypThumbContainer"). height ()-mh; // the height difference between the image and the div

Var h2 = parseInt (ih-h); // the height difference between the content and the div of the border

$ ("# ScrollContent"). hover (function (){

If (ih> h ){

$ ("# YpThumbContainer"). show ("slow ");

$ ("# YpThumb"). find ("img"). height (mh );

$ ("# YpThumb"). show ("slow ");

 

// Begin

Var oDiv = document. getElementById ("ypThumbContainer ");

Var oDiv2 = document. getElementById ("ypThumb ");

Var mouseStart = {};

Var divStart = {};

Var rightStart = {};

Var bottomStart = {};

$ (ODiv2). mousedown (function (ev ){

Var oEvent = ev | event;

MouseStart. x = oEvent. clientX;

MouseStart. y = oEvent. clientY;

DivStart. x = parseint((odiv2).css ("left "));

DivStart. y = parseint((odiv2).css ("top "));

If (oDiv2.setCapture ){

ODiv2.setCapture ();

ODiv2.onmousemove = doDrag3;

ODiv2.onmouseup = stopDrag3;

} Else {

$ (ODiv). bind ("mousemove", doDrag3). bind ("mouseup", stopDrag3 );

}

OEvent. preventDefault ();

});

Function doDrag3 (ev ){

Var oEvent = ev | event;

Vart = oEvent. clientY-mouseStart.y + divStart. y;

If (t <0 ){

T = 0;

} Else if (t >=$ (oDiv). height ()-$ (oDiv2). height ()){

T = $ (oDiv). height ()-$ (oDiv2). height ();

}

((Odiv2).css ("top", t + "px ");

$ ("# Divcontent" 2.16.css ("top",-(t * h2/h1) + "px ");

}

Function stopDrag3 (){

If (oDiv2.releaseCapture ){

ODiv2.releaseCapture ();

ODiv2.onmousemove = null;

ODiv2.onmouseup = null;

} Else {

$ (ODiv). unbind ("mousemove", doDrag3). unbind ("mouseup", stopDrag3 );

}

}

// End

}

}, Function (){

$ ("# YpThumbContainer"). hide ("slow ");

$ ("# YpThumb"). hide ("slow ");

});

});

 

 

</Script>

 

 

The html code is as follows:

 

Html code

<Div style = "width: 150px; overflow: hidden; height: 170px; border: 1px #000 solid; z-index: 1; position: relative; "id =" scrollContent ">

<Div class = "thumbContainer" id = "ypThumbContainer" style = "Z-INDEX: 3; right: 0px; WIDTH: 9px; POSITION: absolute; TOP: 0px; HEIGHT: 170px; border: 1px #000 solid; display: none ">

<Div class = "thumb" id = "ypThumb" style = "POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: # ffd200;">

<br/>

</Div>

</Div>

<Div id = "divcontent" style = "position: absolute;">

Beijing, August 12 (Xinhua)-According to the website of the Supreme People's Court, Sun June, spokesman of the Supreme People's Court, said today that the new marriage law first clarified that the real estate loan purchased by one party in the divorce case should be owned by the Property Rights registrar, however, fair and reasonable compensation should be given to the spouse involved in the loan repayment.

</Div>

</Div>

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.