Chinaunix首页 | 论坛 | 博客
  • 博客访问: 485911
  • 博文数量: 226
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2111
  • 用 户 组: 普通用户
  • 注册时间: 2014-06-20 09:02
个人简介

web web web

文章分类

全部博文(226)

文章存档

2020年(2)

2019年(1)

2018年(3)

2017年(26)

2016年(57)

2015年(60)

2014年(77)

我的朋友

分类: Web开发

2015-04-30 11:24:27


  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  2. <html xmlns="">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>JQuery模拟滚动条</title>


  6. <style type="text/css">
  7. *{margin:0;padding:0;list-style-type:none;}
  8. a,img{border:0;}
  9. body{font:12px/180% Arial, Helvetica, sans-serif, "新宋体";}
  10. /* bd */
  11. .bd{width:362px;margin:0 auto;margin-top:50px;height:600px;}
  12. .top{border:1px solid #ccc;height:573px;width:250px;overflow:hidden;float:left;padding:0 5px 5px 5px;}
  13. .top ul{width:250px;padding-bottom:5px;}
  14. .top ul li{padding:2px;border:1px solid #ccc;font-size:0px;margin-top:5px;}
  15. .top ul .li{margin-top:5px}
  16. .top ul li img{width:244px;height:180px;}
  17. .scroll{width:13px;margin-left:10px;height:578px;float:left;background:#e6e6e6;position:relative; border-radius: 5px;}
  18. .scroll p{width:11px;height:61px;background:#ffffff;cursor:auto;left:1px;top:1px;position:absolute; border-radius:10px;}
  19. </style>

  20. <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
  21. <script type="text/javascript">
  22. $(function(){
  23.     var Bool=false;
  24.     var Scro=$("#scroll");
  25.     var Scrp=$("#p");
  26.     var Scrobd=$("#bd");
  27.     var Scroul=$("#ul");
  28.     var Scrp_Height =Scrp.outerHeight()/2;
  29.     var Num2=Scro.outerHeight()-Scrp.outerHeight();
  30.     var offsetX=0;
  31.     var offsetY=0;
  32.     Scrp.mousedown(function(e){
  33.         Bool=true;
  34.     });
  35.     $(document).mouseup(function(){
  36.         Bool=false;
  37.     });
  38.     $(document).mousemove(function(e){
  39.         if(Bool){
  40.             var Num1= e.clientY - Scro.position().top;
  41.             var y=Num1 - Scrp_Height;
  42.             if(y<=1){
  43.                 Scrll(0);
  44.                 Scrp.css("top",1);
  45.             }else if(y>=Num2){
  46.                 Scrp.css("top",Num2);
  47.                 Scrll(Num2);
  48.             }else{
  49.                 Scrll(y);
  50.             }
  51.         }
  52.     });
  53.     function Scrll(y){
  54.         Scrp.css("top",y);
  55.         Scroul.css("margin-top",-(y/(Scro.outerHeight()-Scrp.outerHeight()))*(Scroul.outerHeight()-Scrobd.height()));
  56.     }
  57.     if(document.getElementById("scroll_bd").addEventListener)
  58.     document.getElementById("scroll_bd").addEventListener('DOMMouseScroll',wheel,true);
  59.     document.getElementById("scroll_bd").onmousewheel=wheel;
  60.     var Distance=Num2*0.1;
  61.     function wheel(e){
  62.         var evt = e || window.event;
  63.         var wheelDelta = evt.wheelDelta || evt.detail;
  64.         if(wheelDelta == -120 || wheelDelta == 3){
  65.             var Distances=Scrp.position().top+Distance;
  66.             if(Distances>=Num2){
  67.                 Scrp.css("top",Num2);
  68.                 Scrll(Num2);
  69.             }else{
  70.                 Scrll(Distances);
  71.             }
  72.             return false;
  73.         }else if (wheelDelta == 120 || wheelDelta == -3){
  74.             var Distances=Scrp.position().top-Distance;
  75.             if(Distances<=1){
  76.                 Scrll(0);
  77.                 Scrp.css("top",1);
  78.             }else{
  79.                 Scrll(Distances);
  80.             }
  81.             return false;
  82.         }
  83.     }
  84. });
  85. </script>

  86. </head>
  87. <body>

  88. <div class="bd" id="scroll_bd">
  89.     <div class="top" id="bd">
  90.         <ul id="ul">
  91.             <li><img src="1.png" /></li>
  92.             <li><img src="2.png" /></li>
  93.             <li><img src="1.png" /></li>
  94.             <li><img src="2.png" /></li>
  95.             <li><img src="1.png" /></li>
  96.             <li><img src="2.png" /></li>
  97.             <li><img src="1.png" /></li>
  98.             <li><img src="2.png" /></li>
  99.             <li><img src="1.png" /></li>
  100.             <li><img src="2.png" /></li>
  101.         </ul>
  102.     </div>
  103.     
  104.     <div class="scroll" id="scroll">
  105.         <p id="p"></p>
  106.     </div>
  107. </div>

  108. </body>
  109. </html>

阅读(1470) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~