JS實(shí)現(xiàn)點(diǎn)擊拉拽輪播圖pc端移動(dòng)端適配
<div class="content">
<button class="left">left</button>
<button class="right">right</button>
<div class="index"></div>
<div class="lists">
<!--<!–width: item的數(shù)量÷3乘以100%–>-->
<div class="box">
<!--width: 1÷item的數(shù)量乘以100%-->
<div class="item">
<img src="a.png" alt="a">
<p>aaa</p>
</div>
<div class="item active">
<img src="b.png" alt="b">
<p>bbb</p>
</div>
<div class="item">
<img src="c.png" alt="c">
<p>ccc</p>
</div>
<div class="item">
<img src="d.png" alt="d">
<p>ddd</p>
</div>
<div class="item">
<img src="e.png" alt="e">
<p>eee</p>
</div>
<div class="item">
<img src="f.png" alt="f">
<p>ffff</p>
</div>
</div>
</div>
</div>
<script>
$(function(){
// 循環(huán)數(shù)據(jù) 假設(shè)有個(gè)數(shù)組,有10條數(shù)據(jù)
// var arr = [1,2,3,4,5,6,7,8,9,10];
// var arr_len = arr.length;
// var box = '<div class="box" style="width: '+arr_len/3*100+'%;"></div>';
// $('.lists').append(box);
// for (var i = 0; i < arr_len;i++){
// var newDiv = document.createElement('div');
// newDiv.innerHTML = '<img src="static/images/search-icon.png"/>'+
// '<p>' +
// (i+1)+
// '</p>';
// newDiv.className = 'item '+(i==1?'active':'');
// newDiv.style = 'width: '+1/arr_len*100+'%;';
// newDiv.onclick = (function(ind) {
// return function () {
// index = ind-1;
// console.log(ind)
// $(".box").animate({left: -index*100/3+"%"})
// $(".item").removeClass('active')
// $($(".item")[index+1]).addClass('active');
// $('.index').text(index+2)
// }
// })(i) ;
// $('.box').append(newDiv);
// }
var arr_len = $('.item').length;
$('.box').css({width: arr_len/3*100+'%'})
$('.item').css({width: 1/arr_len*100+'%'})
$('.item').on('click',function (e) {
var _this = $(e.target);
if (!_this.hasClass('item')){
_this = _this.parents('.item');
}
index = _this.index('.item')-1;
$(".box").animate({left: -index*100/3+"%"})
$(".item").removeClass('active')
$($(".item")[index+1]).addClass('active');
$('.index').text(index+2)
})
var index = 0;
var len = arr_len;
var temp = true;
var pageX,pageY;
$('.content').on('touchstart',function (e) {
var touches = e.originalEvent.targetTouches[0];
pageX = touches.pageX;
pageY = touches.pageY;
}).on('touchmove',function (e) {
var touches = e.originalEvent.targetTouches[0];
if (pageX>touches.pageX+20){
left()
}else if (pageX<touches.pageX-20){
right()
}
})
$(".left").on('click',left)
$(".right").on('click',right)
function left() {
console.log(index,temp,'left')
if (index < len-2&&temp){
index++;
move(index)
}
}
function right() {
if (index > 0&&temp){
index--;
move(index)
}
}
function move(index) {
if (temp){
temp = false;
var left = $(".box").offset().left;
$(".box").animate({left: -index*100/3+"%"},function () {
temp = true;
})
$(".item").removeClass('active')
$($(".item")[index+1]).addClass('active');
$('.index').text(index+2)
}
}
})
.lists {
position: relative;
height: 100px;
overflow: hidden;
}
.box {
position: absolute;
}
.item {
float: left;
background: #008000;
height: 100px;
}
.item img {
width: 20px;
display: block;
margin: 0 auto;
}
.item p {
text-align: center;
}
.item.active {
background: #0000FF;
font-size: 30px;
}
.item.active img {
width: 40px;
}
.item.active p {
font-size: 30px;
}
總結(jié)
以上所述是小編給大家介紹的JS實(shí)現(xiàn)點(diǎn)擊拉拽輪播圖pc端移動(dòng)端適配,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
前端數(shù)組去重面試我會(huì)問這3個(gè)小問題
數(shù)組去重在我們的前端的面試過程中經(jīng)過會(huì)遇到,有一些人可能會(huì)想到一兩種,但是數(shù)據(jù)去重的算法真的太多了,下面這篇文章主要給大家介紹了關(guān)于前端數(shù)組去重面試3個(gè)小問題的相關(guān)資料,需要的朋友可以參考下2023-01-01
JS實(shí)現(xiàn)基本的網(wǎng)頁計(jì)算器功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)基本的網(wǎng)頁計(jì)算器功能,涉及JavaScript事件響應(yīng)及數(shù)值運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
JavaScript高級(jí)教程之如何玩轉(zhuǎn)箭頭函數(shù)
箭頭函數(shù)是在es6中添加的一種規(guī)范,箭頭函數(shù)相當(dāng)于匿名函數(shù),簡化了函數(shù)的定義,下面這篇文章主要給大家介紹了關(guān)于JavaScript高級(jí)教程之如何玩轉(zhuǎn)箭頭函數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
超出JavaScript安全整數(shù)限制的數(shù)字計(jì)算BigInt詳解
這篇文章給大家分享了超出JavaScript安全整數(shù)限制的數(shù)字計(jì)算BigInt的相關(guān)知識(shí)點(diǎn),有興趣的朋友參考學(xué)習(xí)下。2018-06-06
ES6中async函數(shù)與await表達(dá)式的基本用法舉例
async和await是我們進(jìn)行Promise時(shí)的一個(gè)語法糖,async/await為了讓我們書寫代碼時(shí)更加流暢,增強(qiáng)了代碼的可讀性,下面這篇文章主要給大家介紹了關(guān)于ES6中async函數(shù)與await表達(dá)式的基本用法,需要的朋友可以參考下2022-07-07
JavaScript中對(duì)象的不同創(chuàng)建方法
js對(duì)象與一般的面向?qū)ο蟮某绦蛟O(shè)計(jì)語言有所不同的。js中的對(duì)象是基本原型的。下面給大家介紹js中對(duì)象的不同創(chuàng)建方法,非常不錯(cuò),感興趣的朋友一起學(xué)習(xí)吧2016-08-08

