CSS transition属性实现滑动式轮播图

是的,趁着在玩轮播我用transition又写了个滑动式的轮播图,是仿的哔哩哔哩哔哩哔哩。效果看下面咯。

点我转到CodePen

思路

这回我是用JS修改图片的left属性,用CSS的transition来实现动画过程。比如说一个图left: 200px; transition: left 0.3s linear;,然后我用JS把这个图的left改为0,这样图片就有个0.3s的左移动画啦。

滑动式的轮播图图片是怎么动的呢?

clipboard.png

“中间”为轮播图展示区。假设有3张图,我们理下逻辑。

初始的时候,所有图片都位于“右边”。然后图1到“中间” → 图1到“左边” & 图2到“中间” → 图2到“左边” & 图3到“中间” → 图2图3到“右边” & 图1到“中间”。这样一个循环的过程。

我用修改类名的方式来修改图片的left值。没有类名的时候图片位于右侧,有active类时图片位于中间,有left类时位于左侧。

img{
  left: 260px;  /*图片均位于右侧*/
  transition: left 0.3s linear;  /*改变left值实现动画*/
}
img.active{
  left: 0;
}
img.left{
  left: -260px;
}

然后在JS里通过setInterval(code,millisec)来定时执行切换图片的函数。

代码

HTML

<div id="slideshow">
  <!-- 插入轮播的图片们 -->
  <img class="active" src="http://i0.hdslb.com/bfs/archive/1058ca7f23a79f4f0ae0760ad4c08ac9c596f70e.jpg" />
  <img src="http://i0.hdslb.com/bfs/archive/e10fa2ca13cb59b264fce0f9085e1a050cc2dab5.jpg" />
  <img src="http://i0.hdslb.com/bfs/archive/86783a2c790312bb9f5f473896f9d36ec4c1da34.jpg" />
  <!-- 插入轮播的页码们 -->
  <div>
    <span class="active" name="0"></span><span name="1"></span><span name="2"></span>
  </div>
  <!-- 插入图片的描述们 -->
  <p class="active">刀剑乱舞-花丸-</p>
  <p>我太受欢迎了该怎么办</p>
  <p>少女编号</p>
</div>

CSS

*{
  padding: 0; 
  margin: 0;
}
/*-- 轮播图整体样式-- */
#slideshow{
  width: 260px;
  height: 248px;
  margin: 20px auto;
  border-radius: 5px; 
  overflow: hidden;
  position: relative;
}
/*-- 图片样式 --*/
#slideshow img{
  position: absolute;
  top: 0;
  left: 260px;  /*图片均位于右侧*/
  transition: left 0.3s linear;  /*改变left值实现动画*/
}
#slideshow img.active{
  left: 0;
}
#slideshow img.left{
  left: -260px;
}
/*-- 页码样式 --*/
#slideshow div{
  position: absolute;
  bottom: 0;
  width: 100%;
  line-height: 0;
  text-align: center;
  padding: 5px 0;
  background: rgba(0,0,0,0.7);
}
#slideshow span{
  display: inline-block;
  width: 15px;
  height: 10px;
  margin: 0 3px;
  border-radius: 5px;
  background: white;
  transition: width 0.3s;
}
#slideshow span.active{
  width: 25px;
  background: rgb(216,83,127);
}
/*-- 图片描述的样式 --*/
#slideshow p{
  position: absolute;
  bottom: 20px;
  width: 100%;
  line-height: 20px;
  font-size: 14px;
  text-indent: 5px;
  color: white;
  background: rgba(0,0,0,0.4);
  cursor: default;
  opacity: 0;
  transition: opacity 0.3s linear;
}
#slideshow p.active{
  opacity: 1;
}

JS

//---------主角:轮播图函数-------------
function slideshow() {
  var slideshow=document.getElementById("slideshow"),
  imgs=slideshow.getElementsByTagName("img"), //图片们
  pages=slideshow.getElementsByTagName("span"), //页码们
  descrips=slideshow.getElementsByTagName("p"), //描述们
  length=imgs.length, //图片数目
  current=1; //current为当前活跃的图片、页码、描述的编号

  function changeSlide() { //切换图片的函数
    for (var i=0; i<length; i++) {
      if(i==current) {
        imgs[i].className="active";
        pages[i].className="active";
        descrips[i].className="active";
      } else {
        pages[i].className="";
        descrips[i].className="";
        if(i<current) {
          imgs[i].className="left";
        } else {
          imgs[i].className="";
        }
      }
    }
    current++; //自增1
    if(current>=length) {
      current=0;
    }
  }

  //每2s调用changeSlide函数进行图片轮播
  var slideon=setInterval(changeSlide,2000);  

  slideshow.onmouseover=function(){
    clearInterval(slideon); //当鼠标移入时清除轮播事件
  }
  slideshow.onmouseout=function(){
    slideon=setInterval(changeSlide,2000); //当鼠标移出时重新开始轮播事件
  }

  for(var i=0; i<length; i++) {  //定义鼠标移入页码事件
    pages[i].onmouseover=function(){
      current=this.getAttribute("name");  //得到当前鼠标指的页码
      changeSlide();
    }
  }

}

slideshow();

完。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值