<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>輪播圖</title>
<style>
* {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
}
.container {
position: relative;
width: 600px;
height: 400px;
margin: 100px auto 0 auto;
box-shadow: 0 0 5px green;
overflow: hidden;
}
.container .wrap {
position: absolute;
width: 4200px;
height: 400px;
z-index: 1;
transition: all .5s;
}
.container .wrap img {
float: left;
width: 600px;
height: 400px;
}
.container .buttons {
position: absolute;
right: 5px;
bottom: 40px;
width: 150px;
height: 10px;
z-index: 2;
}
.container .buttons span {
margin-left: 5px;
display: inline-block;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: green;
text-align: center;
color: white;
cursor: pointer;
}
.container .buttons span.on {
background-color: red;
}
.container .arrow {
position: absolute;
top: 35%;
color: green;
padding: 0px 14px;
border-radius: 50%;
font-size: 50px;
z-index: 2;
display: none;
}
.container .arrow_left {
left: 10px;
}
.container .arrow_right {
right: 10px;
}
.container:hover .arrow {
display: block;
}
.container .arrow:hover {
background-color: rgba(0, 0, 0, 0.2);
}
</style>
</head>
<body>
<div class="container">
<div class="wrap" style="left: -600px;" id="wrap">
<img src="http://m.beijing.xuefu.com/beijing/img/mcd-index-002xa.png" alt="">
<img src="http://beijing.xuefu.com/g-img/index-banner/index-mwh-bn4.png" alt="">
<img src="http://m.exuefu.com/m/g-img/ez-ban.png" alt="">
</div>
<a class="arrow arrow_left" id="prev"><</a>
<a class="arrow arrow_right" id="next">></a>
</div>
var imgSlides = {
el: function (x, y, z, m) {
//獲得要進行切換值的元素和左右切換按鈕
var offsetTarget = document.getElementById(x) || document.getElementsByClassName(x) || document.querySelector(x);
var prev = document.getElementById(y) || document.getElementsByClassName(y) || document.querySelector(y);
var next = document.getElementById(z) || document.getElementsByClassName(z) || document.querySelector(z);
var initOffsetX = parseInt(offsetTarget.style.left);//獲得元素初始偏移量 -600
var offsetTargetChild = offsetTarget.getElementsByTagName(m);
// 克隆元素/插入元素
var cloneEl = {
firstChild: offsetTarget.childNodes[1].cloneNode(true), //true表示深複製
lastChild: offsetTarget.childNodes[wrap.childNodes.length - 2].cloneNode(true),
cloneInset: function () {
offsetTarget.insertBefore(this.lastChild, offsetTarget.childNodes[0]);
offsetTarget.append(this.firstChild);
// 執行左右切換事件
{
next.onclick = function () { nexSlide(); }
prev.onclick = function () { prevSlide(); }
var offsetXsum = null; //img元素的總寬度
for (var i = 0; i < offsetTargetChild.length; i++) {
offsetXsum += offsetTargetChild[i].offsetWidth; //將每一個img的寬度求和賦值給offsetXsum(總寬度)
}
function nexSlide() {
var newLeft;
offsetTarget.style.left === ("-" + (offsetXsum + initOffsetX) + "px") ? (newLeft = initOffsetX) : (newLeft = parseInt(offsetTarget.style.left) + initOffsetX);
offsetTarget.style.left = newLeft + "px";
}
function prevSlide() {
var newLeft;
offsetTarget.style.left === ("0px" || "" || null || undefined) ? (newLeft = (initOffsetX * (offsetTargetChild.length - 3))) : (newLeft = parseInt(offsetTarget.style.left) - initOffsetX);
offsetTarget.style.left = newLeft + "px";
}
}
}
}
cloneEl.cloneInset();
}
}
imgSlides.el('wrap', 'prev', 'next', 'img');//調用 只須要傳這四個元素便可
</script>
</body>
</html>
swiper導航點擊滑動
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="http://jiaoyu.60design.cn:8081/pZ/js/jquery.1.8.3min.js"></script>
<script src="http://m.yytvip.com.cn/g-js/swiper4.js"></script>
<link rel="stylesheet" href="http://m.beijing.xuefu.com/beijing/css/swiper4.3.5.min.css">
<title>Document</title>
<style type="text/css">
.swiper-container {
width: 100%;
height: 300px;
margin: 50px auto;
}
.swiper-slide {
background:
color:
text-align: center;
line-height: 40px;
height: 40px;
width:100px;
}</style>
</head>
<body>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" >Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 3</div>
</div>
</div>
<script>
var swiper = new Swiper('.swiper-container', {
spaceBetween: 50,
slidesPerView: 2,
centeredSlides: true,
slideToClickedSlide: true,
grabCursor: true,
history: true
});
2.
給導航添加class
function setCurrentSlide(ele, index) {
var els = document.getElementsByClassName('swiper-slide');
for (var i = 0; i < els.length; i++) {
els[i].classList.remove('active')
}
ele.classList.add("active");
}
var swiper1 = new Swiper('.list .swiper-container', {
slidesPerView: 'auto',
paginationClickable: true,
spaceBetween: 0,
freeMode: true,
loop: false,
onTab: function(swiper) {
var n = swiper1.clickedIndex;
}
});
swiper1.slides.each(function(index, val) {
var ele = this;
ele.onclick = function() {
setCurrentSlide(ele, index);
swiper1.slideTo(index);
}
});
</script>
</body>
</html>
複製代碼