jquery使用div實現滾動條效果 – JAVA編程語言程序開發技術文章

查瞭些p實現滾動條的效果,看瞭很多,大多都不符合要求。

要求如下:

1.p大小固定,p中內容大小不固定

2.鼠標移動到內容上時,如果內容太大,則出現滾動條,並可以拖動查看,而內容寬度不變。否則不顯示滾動條。

 

如果純粹的使用p的overflow-y:scroll,那麼內容的寬度會變小。

 

廢話不多說,直接上代碼。

文章中導入jquery-1.4.1.min.js

 

js代碼如下:

 

Js代碼代碼

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

<script type="text/javascript">  

    var check = false;//是否可移動

    $(document).ready(function(){ 

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

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

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

        var h1 = parseInt($("#ypThumbContainer").height())-mh;//圖片與p的高度差值

        var h2 = parseInt(ih – h);//內容與邊框p的高度差值

         

        $("#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 pStart={}; 

                    var rightStart={}; 

                    var bottomStart={}; 

                     

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

                        var oEvent = ev||event; 

                        mouseStart.x = oEvent.clientX; 

                        mouseStart.y = oEvent.clientY; 

                        pStart.x = parseInt($(oDiv2).css("left")); 

                        pStart.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; 

                        var t = oEvent.clientY-mouseStart.y+pStart.y; 

                        if(t < 0){ 

                            t = 0; 

                             

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

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

                        } 

                        $(oDiv2).css("top",t+"px"); 

                        $("#pcontent").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> 

 

 

html代碼如下:

 

Html代碼代碼

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

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

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

        <img class="thumbImg" id="ypThumbImg" height="50" src="images/thumb.gif" width="9" style="cursor:pointer;"/><br /> 

        </p> 

    </p> 

    <p id="pcontent" style="position:absolute;"> 

    中新網8月12日電 據最高人民法院網站消息,最高人民法院新聞發言人孫軍工今日介紹,新婚姻法首次明確離婚案件中一方婚前貸款購買的不動產應歸產權登記方所有,但應對參與還貸的配偶給予公平合理的補償。

    </p> 

</p>   

發佈留言