经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JSJS库框架 » JavaScript » 查看文章
JS编写兼容IE6,7,8浏览器无缝自动轮播
来源:jb51  时间:2018/10/15 8:59:58  对本文有异议

项目要求页面兼容IE6,7,8等浏览器,我们可能会遇到这个轮播效果,轮播板块要求:无限循环、自动轮播和手动切换功能,每一次滚动一小格,网上有很多这类插件,例如:swiper等!

但是很多都是不兼容IE6,7,8这些低级浏览器的,没有办法,只能自己写一个类似的轮播插件

废话不多说,直接上代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <script type="text/javascript" src="js/jquery-1.8.3-20180801.min.js"></script>
  7. <style>
  8. *{margin: 0;padding: 0;}
  9. div{position: relative;width: 1000px;overflow: hidden;height: 100px;line-height:100px;}
  10. ul{position: absolute;list-style: none;overflow: hidden;}
  11. li{float: left;width: 200px;height: 100px;text-align:center;color:#fff;}
  12. a{position: absolute;color:#fff;margin:0 10px;font-size:30px;text-decoration:none;}
  13. </style>
  14. </head>
  15. <body>
  16. <div>
  17. <ul>
  18. <li style="background: red;">1</li>
  19. <li style="background: yellow;">2</li>
  20. <li style="background: blue;">3</li>
  21. <li style="background: black;">4</li>
  22. <li style="background: green;">5</li>
  23. <li style="background: orange;">6</li>
  24. <li style="background: skyblue;">7</li>
  25. <li style="background: blue;">8</li>
  26. <li style="background: green;">9</li>
  27. <li style="background: orange;">10</li>
  28. <li style="background: skyblue;">11</li>
  29. </ul>
  30. <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" class="prev" style="left:0px;"></a>
  31. <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" class="next" style="right:0px;"></a>
  32. </div>
  33. </body>
  34. <script type="text/javascript">
  35. var fli = $("ul li").clone(true);
  36. var oul = $("ul");
  37. oul.append(fli);
  38. oul.width($("ul li").eq(0).width()*$("ul li").length);
  39. var inow = 0;
  40. var timer = null;
  41. $("div").mouseover(function(){
  42. clearInterval(timer);
  43. })
  44. $("div").mouseout(function(){
  45. autoplay();
  46. })
  47. $(".next").click(function(){
  48. if(inow == $("ul li").length/2){
  49. oul.css("left","0px");
  50. inow = 1;
  51. }else{
  52. inow++;
  53. }
  54. var leng = -inow*$("ul li").eq(0).width()+"px";
  55. oul.animate({"left":leng});
  56. })
  57. $(".prev").click(function(){
  58. if(inow == 0){
  59. var ml = -$("ul li").eq(0).width()*($("ul li").length/2)+"px";
  60. oul.css("left",ml);
  61. inow = $("ul li").length/2-1;
  62. }else{
  63. inow--;
  64. }
  65. var leng = -inow*$("ul li").eq(0).width()+"px";
  66. oul.animate({"left":leng});
  67. })
  68. function autoplay(){
  69. timer = setInterval(function(){
  70. if(inow == $("ul li").length/2){
  71. oul.css("left","0px");
  72. inow = 1;
  73. }else{
  74. inow++;
  75. }
  76. console.log(inow);
  77. var leng = -inow*$("ul li").eq(0).width()+"px";
  78. oul.animate({"left":leng});
  79. },2000);
  80. }
  81. autoplay();
  82. </script>
  83. </html>

希望这篇文章能帮到大家,喜欢技术交流的可以关注我,一起交流前端技术。感谢大家对w3xue的支持。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号