jq实现轮播
html代码
<!-- banner开始 --> <div class="banner"> <ul> <li style="display: block;"> <img src="./images/banner.jpg" alt=""> </li> <!-- <li> <img src="./images/banner01.jpg" alt=""> </li> --> <li> <img src="./images/banner03.jpg" alt=""> </li> </ul> <span class="pre"><</span> <span class="next">></span> <div class="dot"> <li style="background-color:#FE6500"></li> <!-- <li></li> --> <li></li> </div> </div> <!-- banner结束 -->
css代码
/* 广告栏css开始 */ .banner { position: relative; } .banner>ul>li { display: none; } .banner>ul>li>img { width: 1080px; } .pre, .next { display: inline-block; width: 64px; text-align: center; height: 64px; line-height: 64px; font-size: 60px; color: #fff; background-color: rgba(49, 99, 159, .1); cursor: pointer; } .pre { position: absolute; left: -3px; top: 50%; margin-top: -39px; } .next { position: absolute; right: -3px; top: 50%; margin-top: -39px; } .dot { display: inline-block; position: absolute; left: 50%; bottom: 26px; margin-left: -25px; } .dot>li { float: left; margin-right: 11px; width: 14px; height: 14px; background-color: #fff; border-radius: 14px; cursor: pointer; } /* 广告栏css结束 */
js代码
引入jq
$(document).ready(function() { // 广告栏的幻灯片开始 var index = 0; var len = $(".banner>ul>li").length; // 前一页 $(".pre").on("click", function() { index--; if (index < 0) { index = len - 1; } $(".banner>ul>li").eq(index).show().siblings().hide(); $(".dot>li").eq(index).css("backgroundColor", "#FE6500").siblings().css( "backgroundColor", "#FFF"); }); // 后一页 $(".next").on("click", function() { index++; if (index > len - 1) { index = 0; } $(".banner>ul>li").eq(index).show().siblings().hide(); $(".dot>li").eq(index).css("backgroundColor", "#FE6500").siblings().css( "backgroundColor", "#FFF"); }); function auto() { $(".next").click(); } // 计时 var s = setInterval(auto, 3000); $(".banner>ul>li,.pre,.next").on("mouseenter", function() { clearInterval(s); }); $(".banner>ul>li,.pre,.next").on("mouseleave", function() { s = setInterval(auto, 3000); }); // 点击下面的小圆点显示对应图片 $(".dot>li").on("mouseenter", function() { i = $(this).index(); $(".dot>li").eq(i).css("backgroundColor", "#FE6500"); }); $(".dot>li").on("mouseleave", function() { i = $(this).index(); if (i != index) { $(".dot>li").eq(i).css("backgroundColor", "#FFF"); } }); $(".dot>li").on("click", function() { clearInterval(s); index = $(this).index(); $(".banner>ul>li").eq(index).show().siblings().hide(); $(".dot>li").eq(index).css("backgroundColor", "#FE6500").siblings().css( "backgroundColor", "#FFF"); }); // 广告栏的幻灯片结束
本文来自博客园,作者:jxweber,转载请注明原文链接:https://www.cnblogs.com/jxweber/p/16636054.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了