JS實(shí)現(xiàn)點(diǎn)擊拉拽輪播圖pc端移動(dòng)端適配
更新時(shí)間:2018年09月05日 14:06:15 作者:A叫我亮王
本文通過實(shí)例代碼給大家介紹了JS點(diǎn)擊拉拽輪播圖pc端移動(dòng)端適配 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
<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)端適配,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
javascript trim 去空格函數(shù)實(shí)現(xiàn)代碼
去除字符串左右兩端的空格,在vbscript里面可以輕松地使用 trim、ltrim 或 rtrim,但在js中卻沒有這3個(gè)內(nèi)置方法,需要手工編寫。下面的實(shí)現(xiàn)方法是用到了正則表達(dá)式,效率不錯(cuò),并把這三個(gè)方法加入String對象的內(nèi)置方法中去。2008-10-10javascript文件加載管理簡單實(shí)現(xiàn)方法
這篇文章主要介紹了javascript文件加載管理簡單實(shí)現(xiàn)方法,可實(shí)現(xiàn)順序加載所有js文件的功能,非常簡單實(shí)用,需要的朋友可以參考下2015-07-07微信小程序?qū)崿F(xiàn)經(jīng)典window掃雷游戲
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)經(jīng)典window掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09JavaScript實(shí)現(xiàn)音樂自動(dòng)切換和輪播
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)音樂自動(dòng)切換和輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11