轮播图(带文字)

带文字的轮播图效果
效果图
CSS样式:

* {padding: 0;margin: 0;font-family: "微软雅黑";font-size: 14px;}
ul,li {list-style: none;}
a {text-decoration: none;color: black;}
.box{width: 800px;height: 400px;margin: 20px auto;overflow: hidden;position: relative;}
.box-1 ul{}
.box-1 ul li{width: 800px;height: 400px;position: relative;overflow: hidden;}
.box-1 ul li img{display:block;width: 800px; height: 400px;}
.box-1 ul li h2{position: absolute;left: 0;bottom: 0;height: 40px;width:300px;background: rgba(125,125,120,.4);text-indent: 2em;
				padding-right:500px ;font-size: 15px;line-height: 40px;text-overflow: ellipsis;overflow: hidden;
				white-space: nowrap;font-weight: normal;color: ghostwhite}
.box-2{position: absolute;right: 10px;bottom: 14px;}
.box-2 ul li{float:left;width: 12px;height: 12px;overflow: hidden; margin: 0 5px; border-radius: 50%;
				background: rgba(0,0,0,0.5);text-indent: 100px;cursor: pointer;}
.box-2 ul .on{background: rgba(255,255,255,0.6);}
.box-3 span{position: absolute;color: white;background: rgba(125,125,120,.3);width: 50px;height: 80px;
				top:50%; font-family: "宋体";line-height: 80px;font-size:60px;margin-top: -40px;
				text-align: center;cursor: pointer;}
.box-3 .prev{left: 10px;}
.box-3 .next{right: 10px;}
.box-3 span::selection{background: transparent;}
.box-3 span:hover{background: rgba(125,125,120,.8);}

HTML结构:

<div class="box">
	<div class="box-1">
		<ul>
			<li>
				<img src="images/1.jpg" alt="这里是第一场图片"></img>
				<h2>这里是第一张图片这里是第一张图片</h2>
			</li>
			<li>
				<img src="images/2.jpg" alt="这里是第二张图片"></img>
				<h2>这里是第二张图片这里是第二张图片</h2>
			</li>
			<li>
				<img src="images/3.jpg" alt="这里是第三张图片"></img>
				<h2>这里是第三张图片这里是第三张图片</h2>
			</li>
			<li>
				<img src="images/4.jpg" alt="这里是第一场图片"></img>
				<h2>这里是第四张图片这里是第四张图片</h2>
			</li>
			<li>
				<img src="images/5.jpg" alt="这里是第一场图片"></img>
				<h2>这里是第五张图片这里是第五张图片</h2>
			</li>
		</ul>
	</div>
	<div class="box-2">
		<ul>
			
		</ul>
	</div>
	<div class="box-3">
		<span class="prev"> < </span>
		<span class="next"> > </span>
	</div>
</div>

javascrip代码:

window.onload = function(){
	function $(param){
		if(arguments[1] == true){
			return document.querySelectorAll(param);
		}else{
			return document.querySelector(param);
		}
	}
	var $box = $(".box");
	var $box1 = $(".box-1 ul li",true);
	var $box2 = $(".box-2 ul");
	var $box3 = $(".box-3");
	var $length = $box1.length;
	
	var str = "";
	for(var i =0;i<$length;i++){
		if(i==0){
			str +="<li class='on'>"+(i+1)+"</li>";
		}else{
			str += "<li>"+(i+1)+"</li>";
		}
	}
	$box2.innerHTML = str;
	
	var current = 0;
	
	var timer;
	timer = setInterval(go,1000);
	function go(){
		for(var j =0;j<$length;j++){
			$box1[j].style.display = "none";
			$box2.children[j].className = "";
		}
		if($length == current){
			current = 0;
		}
		$box1[current].style.display = "block";
		$box2.children[current].className = "on";
		current++;
	}
	
	for(var k=0;k<$length;k++){
		$box1[k].onmouseover = function(){
			clearInterval(timer);
		}
		$box1[k].onmouseout = function(){
			timer = setInterval(go,1000);
		}
	}
	for(var p=0;p<$box3.children.length;p++){
		$box3.children[p].onmouseover = function(){
			clearInterval(timer);
		};
		$box3.children[p].onmouseout = function(){
			timer = setInterval(go,1000);
		}
	}
	
	for(var u =0;u<$length;u++){
		$box2.children[u].index  = u;
		$box2.children[u].onmouseover = function(){
			clearInterval(timer);
			for(var j=0;j<$length;j++){
				$box1[j].style.display = "none";
				$box2.children[j].className = "";
			}
			this.className = "on";
			$box1[this.index].style.display = "block";
			current = this.index +1;
		}
		$box2.children[u].onmouseout = function(){
			timer = setInterval(go,1000);
		}
	}
	
	$box3.children[0].onclick = function(){
		back();
	}
	$box3.children[1].onclick = function(){
		go();
	}
	function back(){
		for(var j =0;j<$length;j++){
			$box1[j].style.display = "none";
			$box2.children[j].className = "";
		}
		if(current == 0){
			current = $length;
		}
		$box1[current-1].style.display = "block";
		$box2.children[current-1].className = "on";
		current--;
	}
}
  • 12
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Swiper轮中添加文字,可以通过以下步骤实现: 1. 在轮的每个slide中添加一个文字容器,例如一个div元素,可以使用绝对定位将其定位在合适的位置。 2. 在CSS中为文字容器设置样式,例如设置背景色、边框、字体大小、颜色等。 3. 在JavaScript中使用Swiper实例的on('slideChange', function () {...})方法,来监听轮的滑动事件。 4. 在滑动事件中动态更新文字容器的内容,例如根据当前slide的索引值来显示相应的文字内容。 以下是一个示例代码: HTML: ``` <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"> <img src="image1.jpg"> <div class="caption">这是第一张片</div> </div> <div class="swiper-slide"> <img src="image2.jpg"> <div class="caption">这是第二张片</div> </div> <div class="swiper-slide"> <img src="image3.jpg"> <div class="caption">这是第三张片</div> </div> </div> </div> ``` CSS: ``` .caption { position: absolute; bottom: 0; left: 0; width: 100%; padding: 10px; background-color: rgba(0, 0, 0, 0.5); color: #fff; font-size: 16px; text-align: center; } ``` JavaScript: ``` var swiper = new Swiper('.swiper-container', { // 其他配置项 }); swiper.on('slideChange', function () { var index = swiper.activeIndex; var caption = document.querySelector('.swiper-slide-active .caption'); caption.innerHTML = '这是第' + (index + 1) + '张片'; }); ``` 在这个示例中,我们在每个slide中添加了一个名为“caption”的div元素,用于显示文字内容。在CSS中,我们将其定位在底部,并设置了样式。在JavaScript中,我们监听了Swiper的slideChange事件,每次滑动时更新当前slide的文字内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值