轮播图详解

我理解的轮播图思想是这样:一些图片按指定顺序依次播放,就像一幅卷轴徐徐打开一般,一手打开,一手又卷起。所以可以将所要轮播的图片“拼在一起”构成一幅大图,在窗口前拉动。

每个黑边框模拟一张需要轮播的图片,一组黑边框构成一幅大图,红色边框模拟窗口,代表每次能显示的区域。

1.构建窗口

.container {
      position: relative;
      width: 1000px;
      height: 406px;
      margin:0 auto 0 auto;
      background-color:#1287b4;
      overflow: hidden;
	  color:white;
    }

<div class="container">

</div>

2.构建一组照片组成的大图

 .container .wrap {
      position: absolute;
      width: 5000px;
      height: 406px;
      z-index: 1;
	 padding:0;
    }
    .container .wrap img {
      
      width: 1000px;
      height: 316px;
	  margin:0;
    }


<div class="wrap" style="left: -1000px;">
			
				<div style="float:left;margin:0;border-color:none;padding:0;width:1000px;">
					<img src="images/校园.jpg" alt="2">	
					<div style="margin-left:7%;">
						<span style="font-size:25px;line-height:25px;margin-top:0;" ><br/>富强 民主 文明 和谐 自由 平等 公正 法治 爱国 敬业 诚信 友善</span>
					</div>
					
				</div>
				<div style="float:left;margin:0;border:none;padding:0;width:1000px;">
					<img src="images/主楼.jpg" alt="0">
					<div style="margin-left:7%;">
						<span style="font-size:25px;line-height:25px;margin-top:0;" ><br/>实事求是</span>
						<span><br/>SEEKING TRUTH FROM FACTS</span>	
					</div>
				</div>	
				<div style="float:left;margin:0;border:none;padding:0;width:1000px;">
					<img src="images/新区.jpg" alt="1">	
					<div style="margin-left:7%;">
						<span style="font-size:25px;line-height:25px;margin-top:0;"><br/>博学 求真 惟恒 创新</span>
						<span><br/>LEARNING EXTENSIVELY PENETRATING COURAGEOUSLY PURSUING PERMANENTLY INNOVATING CONSTANTLY</span>
					</div>
				</div>
				<div style="float:left;margin:0;border:none;padding:0;width:1000px;">
					<img src="images/校园.jpg" alt="2">	
					<div style="margin-left:7%;">
						<span style="font-size:25px;line-height:25px;margin-top:0;"><br/>富强 民主 文明 和谐 自由 平等 公正 法治 爱国 敬业 诚信 友善</span>
					</div>
				</div>
				<div style="float:left;margin:0;border:none;padding:0;width:1000px;">
					<img src="images/主楼.jpg" alt="0">
					<div style="margin-left:7%;">
						<span style="font-size:25px;line-height:25px;margin-top:0;" ><br/>实事求是</span>
						<span><br/>SEEKING TRUTH FROM FACTS</span>	
					</div>
				</div>
				
				
			</div>

3.就像“右手打开卷轴,左手卷起卷轴”,设置模仿左右手功能的按键

.container .arrow {
      position: absolute;
      top: 80%;
      color: white;
      padding:0px 14px;
      border-radius: 50%;
      font-size: 50px;
      z-index: 2;
      
    }
    .container .arrow_left {
      left: 10px;
    }
    .container .arrow_right {
      right: 10px;
    }
    .container:hover .arrow {
      display: block;
    }
    .container .arrow:hover {
      color: gray;
    }

<a href="javascript:;" style="text-decoration: none;" rel="external nofollow" class="arrow arrow_left"><</a>
<a href="javascript:;" style="text-decoration: none;" rel="external nofollow" class="arrow arrow_right">></a>

4.为按键添加功能

 var wrap = document.querySelector(".wrap");
    var next = document.querySelector(".arrow_right");
    var prev = document.querySelector(".arrow_left");
    next.onclick = function () {  // 窗口向右移动
      next_pic();
    }
    prev.onclick = function () {  // 窗口向左移动
      prev_pic();
    }
    function next_pic () {
      var newLeft;
      if(wrap.style.left === "-4000px"){
        newLeft = -2000;
      }else{
        newLeft = parseInt(wrap.style.left)-1000;
      }
      wrap.style.left = newLeft + "px";
    }
    function prev_pic () {
      var newLeft;
      if(wrap.style.left === "0px"){
        newLeft = -2000;
      }else{
        newLeft = parseInt(wrap.style.left)+1000;
      }
      wrap.style.left = newLeft + "px";
    }

5.设置时钟,让窗口自动在“大图”上移动

    function autoPlay () {
      timer = setInterval(function () {
        next_pic();
      },2000);
    }
    autoPlay();

6.当鼠标移到窗口上时,图片暂时停止移动,鼠标移开后,窗口继续移动

 var container = document.querySelector(".container");
    container.onmouseenter = function () {
      clearInterval(timer);
    }
    container.onmouseleave = function () {
      autoPlay();  
    }

效果图:

 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现轮播图的思路通常有以下几步: 1. 准备轮播图的HTML结构和CSS样式。 2. 使用JavaScript获取轮播图的DOM节点,并初始化轮播图的一些参数,比如轮播图的宽度、滑动速度、间隔时间等。 3. 编写事件监听器,监听轮播图前进和后退的按钮点击事件,以及自动轮播的定时器。 4. 在事件监听器中编写轮播图的滑动逻辑,比如用CSS3的transform属性实现轮播图的滑动效果。 5. 处理轮播图的边界问题,比如当轮播图到达最后一张时,要自动跳转到第一张。 6. 可选:为轮播图添加指示器或者控制按钮,让用户可以手动切换轮播图。 具体实现代码可以参考以下示例: HTML: ```html <div class="carousel"> <ul class="carousel-list"> <li><img src="image1.jpg"></li> <li><img src="image2.jpg"></li> <li><img src="image3.jpg"></li> </ul> <div class="prev-btn"><</div> <div class="next-btn">></div> </div> ``` CSS: ```css .carousel { position: relative; overflow: hidden; } .carousel-list { position: relative; width: 300%; left: 0; transition: left 0.5s; } .carousel-list li { float: left; width: 33.33%; } .prev-btn, .next-btn { position: absolute; top: 50%; transform: translateY(-50%); width: 40px; height: 40px; line-height: 40px; text-align: center; cursor: pointer; background-color: rgba(0, 0, 0, 0.5); color: #fff; } .prev-btn { left: 0; } .next-btn { right: 0; } ``` JavaScript: ```javascript var carousel = document.querySelector('.carousel'); var carouselList = carousel.querySelector('.carousel-list'); var prevBtn = carousel.querySelector('.prev-btn'); var nextBtn = carousel.querySelector('.next-btn'); var slideWidth = carousel.clientWidth / 3; var currentIndex = 0; // 初始化轮播图参数 carouselList.style.width = slideWidth * 3 + 'px'; // 监听前进和后退按钮点击事件 prevBtn.addEventListener('click', function() { currentIndex = Math.max(currentIndex - 1, 0); carouselList.style.left = -slideWidth * currentIndex + 'px'; }); nextBtn.addEventListener('click', function() { currentIndex = Math.min(currentIndex + 1, 2); carouselList.style.left = -slideWidth * currentIndex + 'px'; }); // 自动轮播 setInterval(function() { currentIndex = (currentIndex + 1) % 3; carouselList.style.left = -slideWidth * currentIndex + 'px'; }, 2000); ``` 这是一个简单的轮播图实现,具体实现方式会因需求而异,但基本思路都是相似的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值