js實現(xiàn)移動端導航點擊自動滑動效果
更新時間:2017年07月18日 14:10:15 作者:HeavyShell
這篇文章主要為大家詳細介紹了js實現(xiàn)移動端導航點擊自動滑動效果,導航可左右滑動,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現(xiàn)移動端導航點擊滑動效果的具體代碼,供大家參考,具體內容如下
移動端模擬導航可點擊自動滑動 0.1.4。
導航可左右滑動,可點擊邊緣的一個,自動滾動下一個到可視范圍【依賴于iscroll.js】。
廢話不多說直接上代碼:
/* * 移動端模擬導航可點擊自動滑動 0.1.4 * Date: 2017-01-11 * by: xiewei * 導航可左右滑動,可點擊邊緣的一個,自動滾動下一個到可視范圍【依賴于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){}//回調函數(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)<duibi){ myScroll.scrollTo(-this_pos_left+this_prev_width+(this_width*_opt.fingerClick),0, _opt.clickScrollTime); }else if(this_off_right-_wrapper_off_right-(this_width*_opt.fingerClick)<duibi){ myScroll.scrollBy(this_off_right-this_next_width-_wrapper_off_right-(this_width*_opt.fingerClick),0, _opt.clickScrollTime); } } }else{ if(this_index==1){ myScroll.scrollTo(-this_pos_left+this_prev_width,0, _opt.clickScrollTime); }else if(this_index==_obj_li.length-1){ if(this_off_right-_wrapper_off_right>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<duibi){ myScroll.scrollTo(-this_pos_left+this_prev_width,0, _opt.clickScrollTime); }else if(this_off_right-_wrapper_off_right<duibi){ myScroll.scrollBy(this_off_right-this_next_width-_wrapper_off_right,0, _opt.clickScrollTime); } } } } $this_obj.addClass(_opt.className).siblings('li').removeClass(_opt.className); _opt.endClickScroll.call(this,$this_obj); } }); }; })(jQuery);
截圖:
提供demo地址:
更多關于滑動效果的專題,請點擊下方鏈接查看:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
談談我對JavaScript原型和閉包系列理解(隨手筆記8)
這篇文章主要介紹了談談我對JavaScript原型和閉包系列理解(隨手筆記8)的相關資料,需要的朋友可以參考下2015-12-12javascript一個無懈可擊的實例化XMLHttpRequest的方法
由于IE新舊版本以及與其他瀏覽器在ajax技術上的不同,往往需要對不同的瀏覽器做不同的處理,除了笨拙的瀏覽器嗅探技術,大約也就是對象檢測技術可用了。2010-10-10詳解JavaScript面向對象實戰(zhàn)之封裝拖拽對象
本文主要介紹了JavaScript如何用面向對象的方式封裝拖拽對象,通過3種方式來實現(xiàn),幫助讀者更好的理解其原理2021-06-06