【jQuery】全功能轮播图的实现(本文结尾也有javascript版)
轮播图

- 图片自动切换(定时器);
- 鼠标悬停在图片上图片不切换(清除定时器)
- 鼠标悬停在按钮上时显示对应的图片(鼠标悬停事件)
- 鼠标悬停在图片上是现实左右箭头
- 点击左键切换到上一张图片,但图片为第一张时,点击切换到最后一张图片
- 点击右键切换到下一张图片,但图片为最后一张时,点击切换到第一张图片
jquery版本3.4.1
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/reset.css" />
<style>
.big{
position: relative;
width: 450px;
height: 270px;
}
#ulimg{
width: 450px;
height: 270px;
overflow: hidden;
}
#ulimg li{float: left;}
#ulimg ul{
width: 500%;
height: 270px;
display: block;
clear: both;
}
#arrows{
position: absolute;
top:90px;
z-index: 5;
width: 100%;
}
.arrow{
width: 30px;
height: 60px;
background-color: #191E2B;
font-size: 25px;
text-align: center;
line-height: 60px;
border-radius: 10px;
color: #FFFFFF;
float: left;
}
#rarrow{
float: right;
}
.big #arrows{
position: absolute;
z-index: -1;
transition: z-index 1s;
opacity: 1;
cursor: pointer;
}
.btn{
width: 20px;
height: 20px;
background-color: whitesmoke;
font-size: 18px;
text-align: center;
float: left;
margin: 10px;
border-radius: 5px;
}
#btns{
position: absolute;
z-index: 5;
top: 220px;
left: 220px;
cursor: pointer;
}
#btns .btn2{
background: orange;
}
</style>
</head>
<body>
<div class="big">
<div id="ulimg">
<ul>
<li class="now"><img src="img/01.jpg" alt=""/></li>
<li><img src="img/02.jpg" alt=""/></li>
<li><img src="img/03.jpg" alt=""/></li>
<li><img src="img/04.jpg" alt=""/></li>
<li><img src="img/05.jpg" alt=""/></li>
</ul>
</div>
<div id="arrows">
<div class="arrow"><</div>
<div class="arrow" id="rarrow">></div>
</div>
<div id="btns">
<div class="btn btn2">1</div>
<div class="btn">2</div>
<div class="btn">3</div>
<div class="btn">4</div>
<div class="btn">5</div>
</div>
</div>
<script type="text/javascript" src="js/jquery-3.4.1.min.js" ></script>
<script>
var timer=setInterval(change,3000);
var index=1;
function change(){
index++;
if(index>=6){
index=1;
}
$(".btn").removeClass("btn2");
$(".btn").eq(index-1).addClass("btn2");
$("#ulimg ul").animate({
"marginLeft":"-450px"
},1000,function(){
$("#ulimg li:eq(0)").appendTo($(this));
$(this).css("marginLeft",0);
})
}
function lchange(){
$(".btn").removeClass("btn2");
index--;
if(index<=0){
index=5;
}
$(".btn").eq(index-1).addClass("btn2");
$("#ulimg li:eq(4)").prependTo($("#ulimg ul"));
}
function rchange(){
$(".btn").removeClass("btn2");
index++;
if(index>=6){
index=1;
}
$(".btn").eq(index-1).addClass("btn2");
$("#ulimg li:eq(0)").appendTo($("#ulimg ul"));
}
$(".arrow:eq(0)").click(function(){
lchange();
});
$(".arrow:eq(1)").click(function(){
rchange();
});
$(".big").mouseover(function(){
clearInterval(timer);
$("#arrows").css("zIndex",3);
})
$(".big").mouseout(function(){
clearInterval(timer);
timer=setInterval(change,3000);
$("#arrows").css("zIndex",-1);
})
$(".btn").click(function(){
$(".btn").removeClass("btn2");
$(this).addClass("btn2");
var now=$(this).html();
// console.log(now);
// console.log(index);
var btw=now-index;
if(btw<0){
btw=Math.abs(btw);
for(let i=0;i<btw;i++){
lchange();
}
}else if(btw>0){
for(let i=0;i<btw;i++){
rchange();
}
}else{
}
})
</script>
</body>
</html>
javascript版本
html+css
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>轮播图</title>
<link rel="stylesheet" href="css/reset.css" />
<style>
.big{
position: absolute;
width: 450px;
height: 270px;
}
#ulimg li{
position: absolute;
z-index: 1;
opacity: 0;
transition: opacity 1s;
}
#ulimg .now{
position: absolute;
z-index: 2;
opacity: 1;
}
#arrows{
position: absolute;
top:90px;
z-index: 3;
width: 100%;
}
.arrow{
width: 30px;
height: 60px;
background-color: #191E2B;
font-size: 25px;
text-align: center;
line-height: 60px;
border-radius: 10px;
color: #FFFFFF;
float: left;
}
#rarrow{
float: right;
}
.big #arrows{
position: relative;
z-index: 1;
transition: z-index 1s;
opacity: 0;
cursor: pointer;
}
.btn{
width: 20px;
height: 20px;
background-color: whitesmoke;
font-size: 18px;
text-align: center;
float: left;
margin: 10px;
border-radius: 5px;
}
#btns{
position: relative;
z-index: 3;
margin-top: 220px;
margin-left: 220px;
cursor: pointer;
}
#btns .btn2{
background: orange;
}
</style>
</head>
<body>
<div class="big">
<ul id="ulimg">
<li class="now"><img src="img/01.jpg" alt=""/></li>
<li><img src="img/02.jpg" alt=""/></li>
<li><img src="img/03.jpg" alt=""/></li>
<li><img src="img/04.jpg" alt=""/></li>
<li><img src="img/05.jpg" alt=""/></li>
</ul>
<div id="arrows">
<div class="arrow"><</div>
<div class="arrow" id="rarrow">></div>
</div>
<div id="btns">
<div class="btn btn2">1</div>
<div class="btn">2</div>
<div class="btn">3</div>
<div class="btn">4</div>
<div class="btn">5</div>
</div>
</div>
<script src="js/imgs.js"></script>
</body>
</html>
js
var imgs=document.querySelectorAll("#ulimg li");
var big=document.querySelector(".big");
var index=0;
var timer=setInterval(change,1000);
var arrows=document.getElementById("arrows");
var arrowsbtns=document.getElementsByClassName("arrow");
var btns=document.getElementsByClassName("btn");
function resetbtns(){
for(var i=0;i<imgs.length;i++){
imgs[i].className="";
btns[i].className="btn";
btns[i].setAttribute("onclick","changeimg(this)");
}
}
function change(){
resetbtns();
index++;
if(index>=5){
index=0;
}
imgs[index].className="now";
btns[index].className="btn btn2";
}
big.onmouseover=function stop(){
clearInterval(timer);
arrows.style.zIndex=3;
arrows.style.opacity=1;
}
big.onmouseout=function start(){
clearInterval(timer);
timer=setInterval(change,1000);
arrows.style.zIndex=1;
arrows.style.opacity=0;
}
arrowsbtns[0].onclick=function leftChange(){
resetbtns();
index--;
if(index<=-1){
index=4;
}
imgs[index].className="now";
btns[index].className="btn btn2";
}
arrowsbtns[1].onclick=function rightChange(){
resetbtns();
index++;
if(index>=5){
index=0;
}
imgs[index].className="now";
btns[index].className="btn btn2";
}
function changeimg(obj){
resetbtns();
index=obj.innerHTML*1-1;
imgs[index].className="now";
btns[index].className="btn btn2";
}
【jQuery】全功能轮播图的实现(本文结尾也有javascript版)的更多相关文章
- jQuery淡入淡出轮播图实现
大家好我是 只是个单纯的小白,这是人生第一次写博客,准备写的内容是Jquery淡入淡出轮播图实现,在此之前学习JS写的轮播图效果都感觉不怎么好,学习了jQuery里的淡入淡出效果后又写了一次轮播图效果 ...
- 用jQuery写的轮播图
效果图: GitHub地址:https://github.com/123456abcdefg/Javascript 大家可以下载源码查看. 与前一篇写的轮播图实现的效果一致,这个是用jQuery写的, ...
- jQuery实现简易轮播图的效果
(图片素材取自于小米官网) 刚开始接触jQuery的学习,个人觉得如果为了实现多数的动态效果,jQuery的确很简易方便. 下面简易的轮播图效果,还请前辈多多指教~ (努力学习react vue an ...
- 用纯css、JavaScript、jQuery简单的轮播图
完成一个可以自动切换或点击数字的轮播图 HTML代码只需要一个div 包含着一个图片和一个列表,我们主要的思路就是通过点击相应的数字,改变图片的 路径. 有4张图片都在img文件夹里,名称为 img ...
- JQuery实现一个轮播图
1.HTML <div class="banner"> <div class="b_main"> <div class=" ...
- 记录一下自己用jQuery写的轮播图
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- jquery淡入淡出轮播图
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 使用JQuery制作幻灯片(轮播图)
1.首先看一下目录结构 images文件夹放所需要播放的图片. js文件夹放jquery库和main.js 2.html代码: <!DOCTYPE html> <html lang= ...
- jquery 移动端轮播图
<div class="slide"> <div class="slide-box"> <ul class="slide ...
随机推荐
- 推荐算法_CIKM-2019-AnalytiCup 冠军源码解读
最近在帮一初创app写推荐系统,顺便学习一波用户兴趣高速检索的冠军算法. 写总结前贴出冠军代码的git地址:https://github.com/ChuanyuXue/CIKM-2019-Analyt ...
- PHP导出excel文件,第一步先实现PHP模板导出不带数据
今天继续研究PHP导出excel文件,把复杂的事情简单化,一步步实现功能,首先实现模板文件的导出,随后再实现写入数据后导出,最终实现功能,这是基本思路.中间可以加一步,先自己写入数据导出试试,随后再数 ...
- Pytest 单元测试框架
1.pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效 2.安装 pytest pip install pytest 3.验证 pytest 是否安装成功 p ...
- 如何将Altera官方提供的CADENCE.OLB应用于altium Designer中
- Android ListView 代码1
目录 ListView效果 一.ListView的简单用法 二.定制ListView的界面 目标 步骤 1.定义一个实体类作为ListView适配器的适配对象. 2.为ListView的子项指定我们的 ...
- [hdoj5192] 树状数组
枚举所有的区间.对于确定的区间,假设最终的高度为h, 代价是max(∑(Hi−h),∑(h−Hj))(Hi>h,Hj≤h) 等价于max(∑Hi−cnt(i)∗h,cnt(j)∗h−∑Hj) ( ...
- {path:“ /”,expires:7}这一段是什么意思?
1.创建会话cookie: $ .cookie('name','value'); 2.创建到期的cookie,然后7天: $ .cookie('name','value',{到期日:7}); 3.创建 ...
- python 基础知识4 - 字典
1.字典增 #字典增 dic = {'name': '大白', 'age': 20} dic['hight'] = 180 #没有键值对,添加 dic['age'] = 18 #有键值对,覆盖 pri ...
- 如何搭建一个WEB服务器项目(三)—— 实现安卓端联网登录
安卓端调用服务器登录函数进行验证登录 观前提示:本系列文章有关服务器以及后端程序这些概念,我写的全是自己的理解,并不一定正确,希望不要误人子弟.欢迎各位大佬来评论区提出问题或者是指出错误,分享宝贵经验 ...
- 从无到有Springboot整合Spring-data-jpa实现简单应用
本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...