十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹js如何實現(xiàn)移動端導(dǎo)航點擊自動滑動效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)主營固鎮(zhèn)網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,重慶App定制開發(fā),固鎮(zhèn)h5微信小程序開發(fā)搭建,固鎮(zhèn)網(wǎng)站營銷推廣歡迎固鎮(zhèn)等地區(qū)企業(yè)咨詢
JS是JavaScript的簡稱,它是一種直譯式的腳本語言,其解釋器被稱為JavaScript引擎,是瀏覽器的一部分,主要用于web的開發(fā),可以給網(wǎng)站添加各種各樣的動態(tài)效果,讓網(wǎng)頁更加美觀。
移動端模擬導(dǎo)航可點擊自動滑動 0.1.4。
導(dǎo)航可左右滑動,可點擊邊緣的一個,自動滾動下一個到可視范圍【依賴于iscroll.js】。
廢話不多說直接上代碼:
/* * 移動端模擬導(dǎo)航可點擊自動滑動 0.1.4 * Date: 2017-01-11 * by: xiewei * 導(dǎo)航可左右滑動,可點擊邊緣的一個,自動滾動下一個到可視范圍【依賴于iscroll.js】 */ (function ($) { $.fn.navbarscroll = function (options) { //各種屬性、參數(shù) var _defaults = { className:'cur', //當前選中點擊元素的class類名 clickScrollTime:300, //點擊后滑動時間 duibiScreenWidth:0.4, //單位以rem為準,默認為0.4rem scrollerWidth:3, //單位以px為準,默認為3,[僅用于特殊情況:外層寬度因為小數(shù)點造成的不精準情況] defaultSelect:0, //初始選中第n個,默認第0個 fingerClick:0, //目標第0或1個選項觸發(fā),必須每一項長度一致,方可用此項 endClickScroll:function(thisObj){}//回調(diào)函數(shù) } var _opt = $.extend(_defaults, options); this.each(function () { //插件實現(xiàn)代碼 var _wrapper = $(this); var _win = $(window); var _win_width = _win.width(),_wrapper_width = _wrapper.width(),_wrapper_off_left = _wrapper.offset().left; var _wrapper_off_right=_win_width-_wrapper_off_left-_wrapper_width; var _obj_scroller = _wrapper.children('.scroller'); var _obj_ul = _obj_scroller.children('ul'); var _obj_li = _obj_ul.children('li'); var _scroller_w = 0; _obj_li.css({"margin-left":"0","margin-right":"0"}); for (var i = 0; i < _obj_li.length; i++) { _scroller_w += _obj_li[i].offsetWidth; } _obj_scroller.width(_scroller_w+_opt.scrollerWidth); var myScroll = new IScroll('#'+_wrapper.attr('id'), { eventPassthrough: true, scrollX: true, scrollY: false, preventDefault: false }); _init(_obj_li.eq(_opt.defaultSelect)); _obj_li.click(function(){ _init($(this)); }); //解決PC端谷歌瀏覽器模擬的手機屏幕出現(xiàn)莫名的卡頓現(xiàn)象,滑動時禁止默認事件(2017-01-11) _wrapper[0].addEventListener('touchmove',function (e){e.preventDefault();},false); function _init(thiObj){ var $this_obj=thiObj; var duibi=_opt.duibiScreenWidth*_win_width/10,this_index=$this_obj.index(),this_off_left=$this_obj.offset().left,this_pos_left=$this_obj.position().left,this_width=$this_obj.width(),this_prev_width=$this_obj.prev('li').width(),this_next_width=$this_obj.next('li').width(); var this_off_right=_win_width-this_off_left-this_width; if(_scroller_w+2>_wrapper_width){ if(_opt.fingerClick==1){ if(this_index==1){ myScroll.scrollTo(-this_pos_left+this_prev_width,0, _opt.clickScrollTime); }else if(this_index==0){ myScroll.scrollTo(-this_pos_left,0, _opt.clickScrollTime); }else if(this_index==_obj_li.length-2){ myScroll.scrollBy(this_off_right-_wrapper_off_right-this_width,0, _opt.clickScrollTime); }else if(this_index==_obj_li.length-1){ myScroll.scrollBy(this_off_right-_wrapper_off_right,0, _opt.clickScrollTime); }else{ if(this_off_left-_wrapper_off_left-(this_width*_opt.fingerClick)1||this_off_right-_wrapper_off_right<-1){ myScroll.scrollBy(this_off_right-_wrapper_off_right,0, _opt.clickScrollTime); } }else{ if(this_off_left-_wrapper_off_left 截圖:
以上是“js如何實現(xiàn)移動端導(dǎo)航點擊自動滑動效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁名稱:js如何實現(xiàn)移動端導(dǎo)航點擊自動滑動效果
網(wǎng)站路徑:http://m.jiaotiyi.com/article/gjgejh.html