【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 ...
随机推荐
- react-redux的理解
react-redux是辅助redux的,我们正常使用redux是很麻烦的,需要在每个组件中去监听数据变化,执行数据更新等 但是通过react-redux,我们可以简化组件使用公共数据的操作, rea ...
- .Net Core WPF之XAML概述
原文链接,机器翻译,有误处参看原文. XAML overview in WPF 2019/08/08 What is XAML XAML syntax in brief Case and white ...
- OpenCV 3.4.2 Windows系统下的环境搭建(附带opencv_contrib-3.4.2)
前言 当前需要回到Windows平台下进行开发,在win10系统上搭建了编译opencv3.4.2的环境,并添加opencv_contrib-3.4.2的模块,以下是本文所需要的软件以及源码. 系统: ...
- 蓝桥杯备战(一)3n+1问题
[问题描述] 考虑如下的序列生成算法:从整数 n 开始,如果 n 是偶数,把它除以 2:如果 n 是奇数,把它乘 3 加1.用新得到的值重复上述步骤,直到 n = 1 时停止.例如,n = 22 时该 ...
- 选择函数index_select
书中(pytorch入门实战)讲:index_select(input, dim, index),指定维度dim上选取,未有示例. 查到相关资料后, import torch as t # 导入tor ...
- 一句话+两张图搞定JDK1.7HashMap,剩下凑字数
JDK1.7 HashMap一探究竟 HashMap很简单,原理一看散列表,实际数组+链表;Hash找索引.索引若为null,while下一个.Hash对对碰,链表依次查.加载因子.75,剩下无脑扩数 ...
- docker安装之后的配置各种坑
1.docker官网下载安装之后 2.点击 Docker quick start快捷方式 错误提示: windows正在查找bash.exe,也可以手动查找,但是一直找不到. 3.执行 docker- ...
- 用项目强化你的webpack
用你的webpack实现vue-cli 本文围绕前端工程化,用webpack从零搭建一个完整项目的过程 本文核心知识点: webpack的使用 vue组件化思想 Element-UI的使用 别走别走, ...
- 【雕爷学编程】Arduino动手做(51)---触摸按键模块
37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践(动手试试)出真知的理念,以学习和交流为目的,这里准备 ...
- [原创][开源] SunnyUI.Net 开发日志:ListBox 增加跟随鼠标滑过高亮
QQ群里,寸目说,ListBox鼠标移动时,当前行需要焦点,我想了想,不难实现啊 不就是在鼠标移动时重绘Item嘛,何况选中的Item已经改了颜色了. 见UIListBox代码: protected ...