当前位置:Gxlcms > JavaScript > 如何利用html+css+JavaScript来实现简单的图片切换特效

如何利用html+css+JavaScript来实现简单的图片切换特效

时间:2021-07-01 10:21:17 帮助过:43人阅读

这篇文章主要介绍了用html+css+js实现的一个简单的图片切换特效,需要的朋友可以参考下

如图所示。


该图片切换特效实现很简单,而且兼容性很好。

html页面如下

  1. <p class="wrapper">
  2. <p id="focus">
  3. <ul>
  4. <li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/01.jpg" alt="QQ商城焦点图效果下载" /></a></li>
  5. <li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/02.jpg" alt="QQ商城焦点图效果教程" /></a></li>
  6. <li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/03.jpg" alt="jquery商城焦点图效果" /></a></li>
  7. <li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/04.jpg" alt="jquery商城焦点图代码" /></a></li>
  8. <li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/05.jpg" alt="jquery商城焦点图源码" /></a></li>
  9. </ul>
  10. </p>
  11. </p><!-- wrapper end -->
  12. </body>


css样式

  1. <style type="text/css">
  2. * {margin:0; padding:0;}
  3. body {font-size:12px; color:#222; font-family:Verdana,Arial,Helvetica,sans-serif; background:#f0f0f0;}
  4. .clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;}
  5. .clearfix {zoom:1;}
  6. ul,li {list-style:none;}
  7. img {border:0;}
  8. .wrapper {width:800px; margin:0 auto; padding-bottom:50px;}
  9. /* qqshop focus */
  10. #focus {width:800px; height:280px; overflow:hidden; position:relative;}
  11. #focus ul {height:380px; position:absolute;}
  12. #focus ul li {float:left; width:800px; height:280px; overflow:hidden; position:relative; background:#000;}
  13. #focus ul li p {position:absolute; overflow:hidden;}
  14. #focus .btnBg {position:absolute; width:800px; height:20px; left:0; bottom:0; background:#000;}
  15. #focus .btn {position:absolute; width:780px; height:10px; padding:5px 10px; right:0; bottom:0; text-align:right;}
  16. #focus .btn span {display:inline-block; _display:inline; _zoom:1; width:25px; height:10px; _font-size:0; margin-left:5px; cursor:pointer; background:#fff;}
  17. #focus .btn span.on {background:#fff;}
  18. #focus .preNext {width:45px; height:100px; position:absolute; top:90px; background:url(img/sprite.png) no-repeat 0 0; cursor:pointer;}
  19. #focus .pre {left:0;}
  20. #focus .next {right:0; background-position:right top;}
  21. </style>

js脚本

  1. $(function() {
  2. var sWidth = $("#focus").width(); //获取焦点图的宽度(显示面积)
  3. var len = $("#focus ul li").length; //获取焦点图个数
  4. var index = 0;
  5. var picTimer;
  6. //以下代码添加数字按钮和按钮后的半透明条,还有上一页、下一页两个按钮
  7. var btn = "<p class='btnBg'></p><p class='btn'>";
  8. for(var i=0; i < len; i++) {
  9. btn += "<span></span>";
  10. }
  11. btn += "</p><p class='preNext pre'></p><p class='preNext next'></p>";
  12. $("#focus").append(btn);
  13. $("#focus .btnBg").css("opacity",0.5);
  14. //为小按钮添加鼠标滑入事件,以显示相应的内容
  15. $("#focus .btn span").css("opacity",0.4).mouseenter(function() {
  16. index = $("#focus .btn span").index(this);
  17. showPics(index);
  18. }).eq(0).trigger("mouseenter");
  19. //上一页、下一页按钮透明度处理
  20. $("#focus .preNext").css("opacity",0.2).hover(function() {
  21. $(this).stop(true,false).animate({"opacity":"0.5"},300);
  22. },function() {
  23. $(this).stop(true,false).animate({"opacity":"0.2"},300);
  24. });
  25. //上一页按钮
  26. $("#focus .pre").click(function() {
  27. index -= 1;
  28. if(index == -1) {index = len - 1;}
  29. showPics(index);
  30. });
  31. //下一页按钮
  32. $("#focus .next").click(function() {
  33. index += 1;
  34. if(index == len) {index = 0;}
  35. showPics(index);
  36. });
  37. //本例为左右滚动,即所有li元素都是在同一排向左浮动,所以这里需要计算出外围ul元素的宽度
  38. $("#focus ul").css("width",sWidth * (len));
  39. //鼠标滑上焦点图时停止自动播放,滑出时开始自动播放
  40. $("#focus").hover(function() {
  41. clearInterval(picTimer);
  42. },function() {
  43. picTimer = setInterval(function() {
  44. showPics(index);
  45. index++;
  46. if(index == len) {index = 0;}
  47. },4000); //此4000代表自动播放的间隔,单位:毫秒
  48. }).trigger("mouseleave");
  49. //显示图片函数,根据接收的index值显示相应的内容
  50. function showPics(index) { //普通切换
  51. var nowLeft = -index*sWidth; //根据index值计算ul元素的left值
  52. $("#focus ul").stop(true,false).animate({"left":nowLeft},300); //通过animate()调整ul元素滚动到计算出的position
  53. //$("#focus .btn span").removeClass("on").eq(index).addClass("on"); //为当前的按钮切换到选中的效果
  54. $("#focus .btn span").stop(true,false).animate({"opacity":"0.4"},300).eq(index).stop(true,false).animate({"opacity":"1"},300); //为当前的按钮切换到选中的效果
  55. }
  56. });


用到的左右切换图片

以上就是如何利用html+css+JavaScript来实现简单的图片切换特效的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行