移動端滑動效果 swiper 4.0.7

來源:互聯網
上載者:User

標籤:list   pow   水果   arw   lan   oop   etl   mil   san   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>每日優鮮</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<link rel="stylesheet" href="css/swiper.min.css">
<!-- <link rel="stylesheet" href="css/style.css"> -->
<style type="text/css">
html,body {
position:relative;
height:100%;
}
html {
overflow:hidden;
}
* {
padding:0;
margin:0;
}
li {
list-style:none;
}
body {
background:#fff;
font-family:Helvetica Neue,Helvetica,Arial,sans-serif;
font-size:12px;
}
.img img {
width:100%;
display:block;
}
#top {
position:absolute;
top:0;
z-index:5;
width:100%;
background:#fff;
}
#top .addr {
height:36px;
margin:0 auto;
display:block;
}
#top .search {
position:absolute;
top:0;
right:0;
height:36px;
display:block;
}
#nav {
border-bottom:1px solid #ebebeb;
}
#nav .swiper-slide span {
margin:0 5px;
text-align:center;
display:block;
line-height:2.5;
font-size:14px;
color:#333333;
}
#nav .bar {
width:50px;
height:3px;
position:absolute;
bottom:0px;
}
#nav .bar .color {
width:36px;
margin:0 auto;
height:3px;
background:#ff4891;
}
.banner img {
width:100%;
display:block;
}
.banner .swiper-pagination {
left:auto;
right:7px;
bottom:7px;
width:auto;
padding:2px 7px;
border-radius:10px;
color:#fff;
background:rgba(0,0,0,.3);
}
#page {
margin-bottom:50px;
height:100%;
}
#page .slidepage {
height:100%;
}
.scroll {
height:100%;
}
.slidescroll {
height:auto;
}
#footer {
position:fixed;
bottom:0;
z-index:5;
}

</style>
</head>
<body>
<div id="top">
<img src="images/top.png" class="addr">
<img src="images/search.png" class="search">
<div class="swiper-container" id="nav">
<div class="swiper-wrapper">
<div class="swiper-slide">
<span style="color:rgba(255,72,145,1);">熱賣</span></div>
<div class="swiper-slide">
<span>水果</span></div>
<div class="swiper-slide">
<span>乳品</span></div>
<div class="swiper-slide">
<span>零食</span></div>
<div class="swiper-slide">
<span>肉蛋</span></div>
<div class="swiper-slide">
<span>蔬菜</span></div>
<div class="swiper-slide">
<span>酒飲</span></div>
<div class="swiper-slide">
<span>速食</span></div>
<div class="swiper-slide">
<span>熟食</span></div>
<div class="swiper-slide">
<span>水產</span></div>
<div class="swiper-slide">
<span>糧油</span></div>
<div class="swiper-slide">
<span>輕食</span></div>
<div class="swiper-slide">
<span>火鍋</span></div>
<div class="swiper-slide">
<span>日百</span></div>
<div class="bar">
<div class="color"></div>
</div>
</div>
</div>
</div>

<div class="swiper-container" id="page">
<div class="swiper-wrapper">
<!-- 一 -->
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">
<!-- 熱賣 -->
<div class="swiper-container banner">
<div class="swiper-wrapper">
<div class="swiper-slide">
<img src="uploads/banner1.jpg"></div>
<div class="swiper-slide">
<img src="uploads/banner2.jpg"></div>
<div class="swiper-slide">
<img src="uploads/banner3.jpg"></div>
</div>
<div class="swiper-pagination"></div>
</div>

<div class="img">
<img src="images/index1.png">
<img src="images/hot.png">
</div>

<ul class="img">
<li>
<img src="uploads/product1.png"></li>
<li>
<img src="uploads/product2.png"></li>
<li>
<img src="uploads/product3.png"></li>
<li>
<img src="uploads/product4.png"></li>
<li>
<img src="uploads/product5.png"></li>
<li>
<img src="uploads/product3.png"></li>
<li>
<img src="uploads/product4.png"></li>
<li>
<img src="uploads/product5.png"></li>
<li>
<img src="uploads/product3.png"></li>
<li>
<img src="uploads/product4.png"></li>
<li>
<img src="uploads/product5.png"></li>
<li>
<img src="uploads/product3.png"></li>
<li>
<img src="uploads/product4.png"></li>
<li>
<img src="uploads/product5.png"></li>
</ul>


<div class="img">
<img src="images/bottom.png">
</div>


</div>
</div>
</div>
</div>

<!-- 二 -->
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">
<div class="swiper-container banner">
<div class="swiper-wrapper">
<div class="swiper-slide">
<img src="uploads/banner1.jpg"></div>
<div class="swiper-slide">
<img src="uploads/banner2.jpg"></div>
<div class="swiper-slide">
<img src="uploads/banner3.jpg"></div>
</div>
<div class="swiper-pagination"></div>
</div>
<div class="img">
<img src="images/hot.png"></div>
<ul class="img">
<li>
<img src="uploads/product1.png"></li>
<li>
<img src="uploads/product2.png"></li>
<li>
<img src="uploads/product3.png"></li>
<li>
<img src="uploads/product4.png"></li>
<li>
<img src="uploads/product5.png"></li>
<li>
<img src="uploads/product3.png"></li>
<li>
<img src="uploads/product4.png"></li>
<li>
<img src="uploads/product5.png"></li>
<li>
<img src="uploads/product3.png"></li>
<li>
<img src="uploads/product4.png"></li>
<li>
<img src="uploads/product5.png"></li>
<li>
<img src="uploads/product3.png"></li>
<li>
<img src="uploads/product4.png"></li>
<li>
<img src="uploads/product5.png"></li>
</ul>
<div class="img">
<img src="images/bottom.png"></div>
</div>
</div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide3</div></div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide4</div></div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide5</div></div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide6</div></div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide7</div></div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide8</div></div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide9</div></div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide10</div></div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide11</div></div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide12</div>
</div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide13</div>
</div>
</div>
</div>
<div class="swiper-slide slidepage">
<div class="swiper-container scroll">
<div class="swiper-wrapper">
<div class="swiper-slide slidescroll">slide14</div>
</div>
</div>
</div>
</div>
</div>
<div class="img" id="footer"><img src="images/footer.png"></div>
<script src="js/swiper.min.js"></script>
<script>
//暫時設計每個slide大小需要一致
barwidth = 36 //導航粉色條的長度px
tSpeed = 300 //切換速度300ms
var navSwiper = new Swiper(‘#nav‘, {
slidesPerView: 6,
freeMode: true,
on: {
init: function() {
navSlideWidth = this.slides.eq(0).css(‘width‘); //導航字數需要統一,每個導航寬度一致
bar = this.$el.find(‘.bar‘)

bar.css(‘width‘, navSlideWidth)
bar.transition(tSpeed)
navSum = this.slides[this.slides.length - 1].offsetLeft //最後一個slide的位置

clientWidth = parseInt(this.$wrapperEl.css(‘width‘)) //Nav的可視寬度
navWidth = 0
for (i = 0; i < this.slides.length; i++) {
navWidth += parseInt(this.slides.eq(i).css(‘width‘))
}

topBar = this.$el.parents(‘body‘).find(‘#top‘) //頁頭

},

},
});

var pageSwiper = new Swiper(‘#page‘, {
watchSlidesProgress: true,
resistanceRatio: 0,
on: {
//左右拖動內容頁
touchMove: function() {
progress = this.progress

bar.transition(0)
bar.transform(‘translateX(‘ + navSum * progress + ‘px)‘)
//粉色255,72,145灰色51,51,51


for (i = 0; i < this.slides.length; i++) {
slideProgress = this.slides[i].progress
if (Math.abs(slideProgress) < 1) {
r = Math.floor((255 - 51) * (1 - Math.pow(Math.abs(slideProgress), 2)) + 51)
g = Math.floor((72 - 51) * (1 - Math.pow(Math.abs(slideProgress), 2)) + 51)
b = Math.floor((145 - 51) * (1 - Math.pow(Math.abs(slideProgress), 2)) + 51)
navSwiper.slides.eq(i).find(‘span‘).css(‘color‘, ‘rgba(‘ + r + ‘,‘ + g + ‘,‘ + b + ‘,1)‘)
}
}
},

transitionStart: function() {
//內容頁id
activeIndex = this.activeIndex
console.log(activeIndex);
activeSlidePosition = navSwiper.slides[activeIndex].offsetLeft
//釋放時導航粉色條移動過渡
bar.transition(tSpeed)
bar.transform(‘translateX(‘ + activeSlidePosition + ‘px)‘)
//釋放時文字變色過渡
navSwiper.slides.eq(activeIndex).find(‘span‘).transition(tSpeed)
navSwiper.slides.eq(activeIndex).find(‘span‘).css(‘color‘, ‘rgba(255,72,145,1)‘)
if (activeIndex > 0) {
navSwiper.slides.eq(activeIndex - 1).find(‘span‘).transition(tSpeed)
navSwiper.slides.eq(activeIndex - 1).find(‘span‘).css(‘color‘, ‘rgba(51,51,51,1)‘)
}
if (activeIndex < this.slides.length) {
navSwiper.slides.eq(activeIndex + 1).find(‘span‘).transition(tSpeed)
navSwiper.slides.eq(activeIndex + 1).find(‘span‘).css(‘color‘, ‘rgba(51,51,51,1)‘)
}
//導航置中
navActiveSlideLeft = navSwiper.slides[activeIndex].offsetLeft //activeSlide距左邊的距離

navSwiper.setTransition(tSpeed)
if (navActiveSlideLeft < (clientWidth - parseInt(navSlideWidth)) / 2) {
navSwiper.setTranslate(0)
} else if (navActiveSlideLeft > navWidth - (parseInt(navSlideWidth) + clientWidth) / 2) {
navSwiper.setTranslate(clientWidth - navWidth)
} else {
navSwiper.setTranslate((clientWidth - parseInt(navSlideWidth)) / 2 - navActiveSlideLeft)
}

},
}
});

navSwiper.$el.on(‘touchstart‘, function(e) {
e.preventDefault() //去掉按壓陰影
})

//點擊nav 標題
navSwiper.on(‘tap‘, function(e) {

clickIndex = this.clickedIndex
clickSlide = this.slides.eq(clickIndex)

pageSwiper.slideTo(clickIndex, 0);
this.slides.find(‘span‘).css(‘color‘, ‘rgba(51,51,51,1)‘);
clickSlide.find(‘span‘).css(‘color‘, ‘rgba(255,72,145,1)‘);
})

//內容上下滾動
var scrollSwiper = new Swiper(‘.scroll‘, {
//65是頭部的高
//36是top地址和搜尋的高

slidesOffsetBefore: 72,
direction: ‘vertical‘,
freeMode: true,
slidesPerView: ‘auto‘,

/*mousewheel: {
releaseOnEdges: true,
},
on: {

//上下滑動
touchMove: function() {

if (this.translate > 72 - 36 && this.translate < 72) {
topBar.transform(‘translateY(‘ + (this.translate - 72) + ‘px)‘);
}

},
//開始滑動
touchStart: function() {
startPosition = this.translate
//上下的位置
console.log(startPosition);
},

//滑動成功後的
touchEnd: function() {
topBar.transition(tSpeed)
console.log(topBar);
if (this.translate > 36 && this.translate < 72 && this.translate < startPosition) {
topBar.transform(‘translateY(-36px)‘);
for (sc = 0; sc < scrollSwiper.length; sc++) {
if (scrollSwiper[sc].translate > 36) {
scrollSwiper[sc].setTransition(tSpeed);
scrollSwiper[sc].setTranslate(36)
}
}
}
if (this.translate > 36 && this.translate < 72 && this.translate > startPosition) {
topBar.transform(‘translateY(0px)‘);
for (sc = 0; sc < scrollSwiper.length; sc++) {
if (scrollSwiper[sc].translate < 72 && scrollSwiper[sc].translate > 0) {
scrollSwiper[sc].setTransition(tSpeed);
scrollSwiper[sc].setTranslate(72)
}
}
}
},

transitionStart: function() {

topBar.transition(tSpeed)
if (this.translate < 72 - 36) {
topBar.transform(‘translateY(-36px)‘);
if (scrollSwiper) {
for (sc = 0; sc < scrollSwiper.length; sc++) {
if (scrollSwiper[sc].translate > 36) {
scrollSwiper[sc].setTransition(tSpeed);
scrollSwiper[sc].setTranslate(36)
}
}
}

} else {
topBar.transform(‘translateY(0px)‘);
if (scrollSwiper) {
for (sc = 0; sc < scrollSwiper.length; sc++) {
if (scrollSwiper[sc].translate < 72 && scrollSwiper[sc].translate > 0) {
scrollSwiper[sc].setTransition(tSpeed);
scrollSwiper[sc].setTranslate(72)
}
}
}
}
},
}*/

})

//熱賣
var bannerSwiper = new Swiper(‘.banner‘, {
loop: true,
pagination: {
el: ‘.swiper-pagination‘,
type: ‘fraction‘,
renderFraction: function(currentClass, totalClass) {
return ‘<span class="‘ + currentClass + ‘"></span>‘ + ‘/‘ + ‘<span class="‘ + totalClass + ‘"></span>‘;
},
},
});
</script>
</body>
</html>

移動端滑動效果 swiper 4.0.7

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.