Giter Club home page Giter Club logo

seamless-scroll's People

Watchers

yinsong avatar

seamless-scroll's Issues

首尾各补一个+jquery 实现的无缝轮播banner

html

<!DOCTYPE HTML>
<html>
	<head>
		<title>无缝滚动轮播</title>
		<meta charset="utf-8">
		<meta name="Author" content="Jesonhu">
		<style type='text/css'>
			*{ margin:0; padding:0;}
			#banner{
				position:relative;
				width:520px;
				height:280px;
				margin:20px auto;
				border:1px solid red;
			}
			#banner .pic{
				overflow:hidden;
				width:100%;
				height:100%;
			}
			li{
				list-style:none;
			}
			#banner .pic ul{
				overflow:hidden;
				width:1000%;
				margin-left:-520px;
			}
			#banner .pic ul li{
				float:left;
			}

			#banner .tab{
				position:absolute;
				bottom:10px;
				left:50%;
				width:70px;
				height:14px;
				margin-left:-35px;
				border-radius:7px;
				background:rgba(255,255,255,0.5);
			}
			#banner .tab ul li{
				float:left;
				width:10px;
				height:10px;
				margin:2px 2px;
				border-radius:100%;
				cursor:pointer;
				background:#999;
			}
			#banner .tab ul li.on{
				background:#f60;
			}
			#banner .btn{
				display:none;
			}
			#banner .btn div{
				position:absolute;
				top:50%;
				width:40px;
				height:40px;
				margin-top:-20px;
				color:#fff;
				font-size:26px;
				font-weight:bold;
				text-align:center;
				line-height:40px;
				cursor:pointer;
				background:rgba(0,0,0,0.3);
			}
			#leftBtn{
				left:5px;
			}
			#rightBtn{
				right:5px;
			}
			#banner .btn div.hover{
				background:rgba(0,0,0,0.6);
			}

		</style>
	</head>
	<body>
		
		<div id="banner">
			
			<div class="pic">
				<ul>
					<li><a href=""><img src="img/5.jpg" /></a></li>
					<li><a href=""><img src="img/1.jpg" /></a></li>
					<li><a href=""><img src="img/2.jpg" /></a></li>
					<li><a href=""><img src="img/3.jpg" /></a></li>
					<li><a href=""><img src="img/4.jpg" /></a></li>
					<li><a href=""><img src="img/5.jpg" /></a></li>
					<li><a href=""><img src="img/1.jpg" /></a></li>
				</ul>
			</div>

			<div class="tab">
				<ul>
					<li class="on"></li><li></li><li></li><li></li><li></li>
				</ul>
			</div>

			<div class="btn">
				<div id="leftBtn">&lt;</div>
				<div id="rightBtn">&gt;</div>
			</div>

		</div>

		<script type="text/javascript" src="js/jquery-1.12.1.min.js"></script>
		<script src="./js/mScroll.js"></script>
	</body>
</html>

js

/**
 * 点击无缝轮播效果实现
 * @auther Jesonhu
 * @update 20180405
 * @desc 功能:
 *       1.无缝滚动
 *       2.点击频率限制
 */
;
(function ($) {
  $(function () {
    const $banner = $('#banner');
    const $imgUl = $('#banner .pic ul');
    const $imgLi = $('#banner .pic li');
    const imgWidth = $imgLi.width();
    const $tabLi = $('#banner .tab li');
    const tabLiLength = $tabLi.length;
    const $btnDiv = $('#banner .btn div');
    const FAST_HANDLER_TIME = 350;
    const DURATION = 3000;
    let index = 0;
    let prevHandlerTime = new Date();
    let autoPlayTimer = null;

    /**
     * 显示下一张图片
     */
    function showNextImg() {
      index++;
      $imgUl.animate({
        marginLeft: -imgWidth * (index + 1) + 'px'
      }, 300, function () {
        if (index >= tabLiLength) {
          index = 0;
          $imgUl.css({
            marginLeft: -imgWidth + 'px'
          });
          $tabLi.eq(index).addClass('on').siblings().removeClass('on');
        }
      });
      $tabLi.eq(index).addClass('on').siblings().removeClass('on');
    }

    /**
     * 轮播自动播放 
     */
    function autoPlay() {
      autoPlayTimer = setInterval(() => {
        showNextImg();
      }, DURATION);
    }
    autoPlay();

    /**
     * tab点击切换
     * @desc Tips: jQuery回调不能使用箭头函数, 如果使用箭头函数
     *       内部this指向#document。
     */
    $tabLi.on('click', function () {
      index = $(this).index();
      $tabLi.eq(index).addClass('on').siblings().removeClass('on');
      // FIXME: 这里采用css3 transform体验性会更好
      $imgUl.animate({
        marginLeft: -imgWidth * (index + 1) + 'px'
      }, 300);
    });

    /**
     * 左右按钮点击 
     */
    $btnDiv.on('click', function (e) {
      const clickIndex = $(this).index();
      const nowHandlerTime = new Date();
      if ((nowHandlerTime - prevHandlerTime) > FAST_HANDLER_TIME) {
        prevHandlerTime = new Date();

        // 点击了左按钮
        if (clickIndex === 0) {
          index--;
          $imgUl.animate({
            marginLeft: -imgWidth * (index + 1) + 'px'
          }, 300, function () {
            if (index < 0) {
              index = tabLiLength - 1;

              $imgUl.css({
                marginLeft: -imgWidth * (index + 1) + 'px'
              });
              // methods2
              // $imgUl.css({marginLeft: -imgWidth * tabLiLength + 'px'});

              $tabLi.eq(index).addClass('on').siblings().removeClass('on');
            }
          });
        } else {
          showNextImg();
        }
        $tabLi.eq(index).addClass('on').siblings().removeClass('on');
      }
    });

    /**
     * 鼠标悬浮到轮播区域 
     */
    $banner.on('mouseenter', function () {
      $btnDiv.parent().show();
      clearInterval(autoPlayTimer);
    });
    /**
     * 鼠标离开轮播区域 
     */
    $banner.on('mouseleave', function () {
      $btnDiv.parent().hide();
      autoPlay();
    });

  });
})(jQuery);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.