关于轮播图的练习和遇到的问题
来源:互联网 发布:正交矩阵的性质 编辑:程序博客网 时间:2024/05/23 01:59
这是关于轮播自己写的一个小练习,先说说思路z。
1.先用html+css将基本的框架构建出来,然后圆点和图片绑定,绑定的方式使用data-x属性来控制图片位移的距离
2.用定时器将图片轮播,在这需要主要的是最后一个圆点返回第一个圆点时的情况。
遇到的困难:鼠标放上去的时候定时器停止,移开的时候轮播开始。
分析:首先,当网页打开的时候轮播是开始的,所以需要执行一次bb(),然后鼠标放上去的时候清除定时器
clearInterval(t); 鼠标离开的时候再次运行一边bb()。轮播的使用还是不够熟练,有写的不好的地方希望各位前辈能指点一下,谢谢。
<!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div.lb{
position: relative;
width: 670px;
height: 280px;
margin-left: 25%;
overflow: hidden;
}
div.img1{
position: absolute;
width: 2680px;
height: 280px;
border: 1px solid black;
left: 0;
top: 0;
transition: all 2s;
}
div.img1 img{
float: left;
}
.yd{
position: absolute;
bottom: 10px;
right: 20px;
}
.yd a{
display: inline-block;
width: 20px;
height: 20px;
background-color: orange;
text-decoration: none;
text-align: center;
line-height: 20px;
color: black;
border-radius: 10px;
}
#sel{
background-color: red;
}
</style>
</head>
<body>
<div class="lb">
<div class="img1">
<img src="a1.jpg" alt="">
<img src="a2.jpg" alt="">
<img src="a3.jpg" alt="">
<img src="a4.jpg" alt="">
</div>
<!-- 小圆点 -->
<div class="yd">
<a href="#" data-x="0" id="sel">1</a>
<a href="#" data-x="-670">2</a>
<a href="#" data-x="-1340">3</a>
<a href="#" data-x="-2010">4</a>
</div>
</div>
<script>
var a=document.querySelectorAll("div.yd a");
var img1=document.querySelector("div.img1")
for(var i=0;i<a.length;i++){
a[i].onmouseover=function(){
var sel=document.querySelector("#sel");
sel.id="";
this.id="sel";
this.parentNode.previousElementSibling.style.left = this.getAttribute("data-x")+"px"
clearInterval(t);
}
a[i].onmouseout=function(){
bb()
}
}
img1.onmouseover=function(){
clearInterval(t);
}
img1.onmouseout=function(){
bb()
}
bb()
// 定时器部分
function bb(){
t=setInterval(function(){
var sel=document.querySelector("#sel");
if(sel.nextElementSibling!=null){
sel.id="";
sel.nextElementSibling.id="sel";
sel.parentNode.previousElementSibling.style.left = sel.nextElementSibling.getAttribute("data-x")+"px"
}else{
sel.id="";
a[0].id="sel";
sel.parentNode.previousElementSibling.style.left = a[0].getAttribute("data-x")+"px"
}
},3000)
}
</script>
</body>
</html><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
div.lb{
position: relative;
width: 670px;
height: 280px;
margin-left: 25%;
overflow: hidden;
}
div.img1{
position: absolute;
width: 2680px;
height: 280px;
border: 1px solid black;
left: 0;
top: 0;
transition: all 2s;
}
div.img1 img{
float: left;
}
.yd{
position: absolute;
bottom: 10px;
right: 20px;
}
.yd a{
display: inline-block;
width: 20px;
height: 20px;
background-color: orange;
text-decoration: none;
text-align: center;
line-height: 20px;
color: black;
border-radius: 10px;
}
#sel{
background-color: red;
}
</style>
</head>
<body>
<div class="lb">
<div class="img1">
<img src="a1.jpg" alt="">
<img src="a2.jpg" alt="">
<img src="a3.jpg" alt="">
<img src="a4.jpg" alt="">
</div>
<!-- 小圆点 -->
<div class="yd">
<a href="#" data-x="0" id="sel">1</a>
<a href="#" data-x="-670">2</a>
<a href="#" data-x="-1340">3</a>
<a href="#" data-x="-2010">4</a>
</div>
</div>
<script>
var a=document.querySelectorAll("div.yd a");
var img1=document.querySelector("div.img1")
for(var i=0;i<a.length;i++){
a[i].onmouseover=function(){
var sel=document.querySelector("#sel");
sel.id="";
this.id="sel";
this.parentNode.previousElementSibling.style.left = this.getAttribute("data-x")+"px"
clearInterval(t);
}
a[i].onmouseout=function(){
bb()
}
}
img1.onmouseover=function(){
clearInterval(t);
}
img1.onmouseout=function(){
bb()
}
bb()
// 定时器部分
function bb(){
t=setInterval(function(){
var sel=document.querySelector("#sel");
if(sel.nextElementSibling!=null){
sel.id="";
sel.nextElementSibling.id="sel";
sel.parentNode.previousElementSibling.style.left = sel.nextElementSibling.getAttribute("data-x")+"px"
}else{
sel.id="";
a[0].id="sel";
sel.parentNode.previousElementSibling.style.left = a[0].getAttribute("data-x")+"px"
}
},3000)
}
</script>
</body>
</html>
0 0
- 关于轮播图的练习和遇到的问题
- 【项目练习】遇到的问题和解决方法
- 练习时遇到的问题
- 练习firstMFC遇到的问题
- 练习flask遇到的问题
- 在练习“==”和“equals”时遇到的问题
- c++练习中遇到的问题和解决
- 今天练习UserControl遇到的问题
- 今天练习.NET时遇到的问题
- Java数组练习中遇到的问题
- Qt 练习遇到的一些问题
- java练习中遇到的问题记录
- 关于BadgeView遇到的一个奇怪的问题和解决办法
- 遇到的一个关于 this 和toString()的问题
- 关于SearchView的使用和遇到的问题
- 关于css遇到的问题
- 关于混编遇到的问题
- 关于cocoapods遇到的问题
- DB2获取上月底,上年底,上季度底
- jzoj 5050 颜色树
- 20170503 大西北的山
- 最短路径算法 模板_Dijkstra_Bellman.ford_Floyd_spfa
- ViewPager结合Fragment 实现的页面切换
- 关于轮播图的练习和遇到的问题
- linux eval 命令的使用
- asp.net mvc 学习总结
- 5-18 二分法求多项式单根 (20分)
- 插入排序
- HDU 5971 Wrestling Match(染色法二分图 OR 并查集)
- JZOJ 1329——旅行【贪心】
- 使用Messenger实现IPC
- 扑克牌顺序