1.首先来看一下html文件的代码:
<div class="main">
<div class="main-wrapper">
<div class="carousel-map" id="banner-group">
<ul class="carousel-img" id="banner-ul">
<li><a href="#"><img src="./../../dist/images/news.min.jpg" alt=""></a></li>
<li><a href="#"><img src="./../../dist/images/news01.min.jpg" alt=""></a></li>
<li><a href="#"><img src="./../../dist/images/news02.min.jpg" alt=""></a></li>
<li><a href="#"><img src="./../../dist/images/news03.min.jpg" alt=""></a></li>
</ul>
<span class="arrow left-arrow"><</span>
<span class="arrow right-arrow">></span>
</div>
<div class="header-sec">
</div>
</div>
</div>
- 然后来看一下css文件中的样式:
.main {
margin-top: $headerHeight + 10px;
.main-wrapper {
width: $mainWrapperWidth;
margin: 0 auto;
.carousel-map {
overflow: hidden;
height: $headerSecHeight;
width: 100%;
margin: 0 auto;
position: relative;
.carousel-img {
width: $carouselImgTotalWidth;
height: $carouselImgHeight;
margin: 0 auto;
position: absolute;
li {
float: left;
list-style: none;
a {
text-decoration: none;
img {
width: $carouselImgWidth;
height: $carouselImgHeight;
}
}
}
}
.arrow{
font-family: "Helvetica,Helvetica Neue,Arial,sans-serif";
font-size: 80px;
color: #ffffff;
// 将其设置为绝对定位,相对于bannerGroup,就可以显示出箭头
position: absolute;
overflow: hidden;
cursor: pointer;
// 默认情况下,不显示
display: none;
}
.left-arrow{
top: 35%;
margin-left: 35px;
}
.right-arrow {
top: 35%;
margin-left: 1220px;
}
}
}
}
- 接下来就是js文件中实现控制轮播图的运动,
function Banner() {
this.index = 0;
this.leftArrow = $(".leftArrow");
this.rightArrow = $(".rightArrow");
this.bannerGroup = $("#banner-group");
this.bannerUl = $("#banner-ul");
this.liList = this.bannerUl.children("li");
this.bannerCount = this.liList.length;
}
Banner.protype.loop = function() {
var self = this;
self.timer = setIntervel(function(){
if(self.index >= self.bannerCount - 1){
self.index = 0;
}else {
self.index++;
}
self.bannerUl.animate({"left": -1300*self.index},500)
},2000);
Banner.protype.listenBannerHover = function() {
var self = this;
self.bannerUl.hover(function() {
clearIntervel(self.timer);
self.toggleArrow(true);
},function(){
self.loop();
self.toggleArrow(false);
});
}
Banner.protype.toggleArrow = function(isShow) {
var self = this;
if(isShow){
self.leftArrow.show();
self.rightArrow.show();
}else{
self.leftArrow.hide();
sef.rightArrow.hide();
}
}
Banner.protype.listenArrowClick = function () {
var self = this;
self.leftArrow.click(function() {
if(self.index === 0){
self.index = self.bannerCount - 1;
}else {
self.index--;
}
self.bannerUl.animate({"left": -1300*self.index},500)
});
self.rightArrow.click(function() {
if(self.index === self.bannerCount - 1){
self.index = 0;
}else{
self.index++;
}
self.bannerUl.animate({"left": -1300*self.index},500)
});
}
Banner.protype.run = function() {
this.loop();
this.listenBannerHover();
this.listenArrowClick();
}
$(function() {
var banner = new Banner();
banner.run();
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)