使用Css,JavaScript实现轮播效果图

   css详细代码:

<style>

        .container {

          width: 980px;

          margin: 0 auto;

          overflow: hidden;

          position: relative;

        }

        .content {

          width: calc(980px*8);

          height: 330px;

          margin-left: 0px;

          transition: margin-left 1s;

        }

    

        .container:hover>.tab {

          opacity: 1;

          transition: opacity 1s;

        }

    

        .content>img {

          float: left;

          width: 980px;

          height: 330px;

        }

    

        .tab {

          position: absolute;

          top: calc(330px/2 - 20px);

          width: 100%;

          opacity: 0;

        }

    

        .tab>span {

          display: block;

          width: 40px;

          height: 40px;

          text-align: center;

          line-height: 40px;

          background-color: rgba(203, 203, 203, 0.47);

          font-size: 30px;

          color: #fff;

          cursor: pointer;

        }

    

        .tab>span:first-child {

          float: left;

        }

    

        .tab>span:last-child {

          float: right;

        }

      </style>

 

html详细代码:

<div class="container">

        <div class="content">

          <img src="1.jpg" />

          <img src="2.jpg" />

          <img src="3.jpg" />

          <img src="4.jpg" />

          <img src="5.jpg" />

          <img src="6.jpg" />

          <img src="7.jpg" />

          <img src="8.jpg" />

        </div>

        <div class="tab">

          <span οnclick="slideImg(0)">&lt;</span>

          <span οnclick="slideImg(1)">&gt;</span>

        </div>

      </div>

 

JS详细代码:

 <script>

    

        /**

         * @method slideImg

         * @param d :direction left 0, right 1.

         */

        function slideImg(d) {

          var oDiv = document.getElementsByClassName("content")[0];

          var mLeft = oDiv.style.marginLeft;

          if (mLeft == "") {

            mLeft = "0px"

          }

          mLeft = parseInt(mLeft);

          // 向左滑动

          if (d === 0) {

            mLeft -= 980;

            if (mLeft < -6860) {

              mLeft = 0;

            }

            oDiv.style.marginLeft = mLeft + "px";

    

          } else {

            // 向右滑动

            mLeft += 980;

            if (mLeft > 0) {

              mLeft = -6860;

            }

            oDiv.style.marginLeft = mLeft + "px";

          }

          //    console.log(mLeft)

        }

      </script>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值