js實(shí)現(xiàn)移動(dòng)端導(dǎo)航點(diǎn)擊自動(dòng)滑動(dòng)效果
本文實(shí)例為大家分享了js實(shí)現(xiàn)移動(dòng)端導(dǎo)航點(diǎn)擊滑動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
移動(dòng)端模擬導(dǎo)航可點(diǎn)擊自動(dòng)滑動(dòng) 0.1.4。
導(dǎo)航可左右滑動(dòng),可點(diǎn)擊邊緣的一個(gè),自動(dòng)滾動(dòng)下一個(gè)到可視范圍【依賴于iscroll.js】。
廢話不多說直接上代碼:
/*
* 移動(dòng)端模擬導(dǎo)航可點(diǎn)擊自動(dòng)滑動(dòng) 0.1.4
* Date: 2017-01-11
* by: xiewei
* 導(dǎo)航可左右滑動(dòng),可點(diǎn)擊邊緣的一個(gè),自動(dòng)滾動(dòng)下一個(gè)到可視范圍【依賴于iscroll.js】
*/
(function ($) {
$.fn.navbarscroll = function (options) {
//各種屬性、參數(shù)
var _defaults = {
className:'cur', //當(dāng)前選中點(diǎn)擊元素的class類名
clickScrollTime:300, //點(diǎn)擊后滑動(dòng)時(shí)間
duibiScreenWidth:0.4, //單位以rem為準(zhǔn),默認(rèn)為0.4rem
scrollerWidth:3, //單位以px為準(zhǔn),默認(rèn)為3,[僅用于特殊情況:外層寬度因?yàn)樾?shù)點(diǎn)造成的不精準(zhǔn)情況]
defaultSelect:0, //初始選中第n個(gè),默認(rèn)第0個(gè)
fingerClick:0, //目標(biāo)第0或1個(gè)選項(xiàng)觸發(fā),必須每一項(xiàng)長(zhǎng)度一致,方可用此項(xiàng)
endClickScroll:function(thisObj){}//回調(diào)函數(shù)
}
var _opt = $.extend(_defaults, options);
this.each(function () {
//插件實(shí)現(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端谷歌瀏覽器模擬的手機(jī)屏幕出現(xiàn)莫名的卡頓現(xiàn)象,滑動(dòng)時(shí)禁止默認(rè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地址:
更多關(guān)于滑動(dòng)效果的專題,請(qǐng)點(diǎn)擊下方鏈接查看:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS中用三種方式實(shí)現(xiàn)導(dǎo)航菜單中的二級(jí)下拉菜單
- JS實(shí)現(xiàn)選中當(dāng)前菜單后高亮顯示的導(dǎo)航條效果
- 一個(gè)js控制的導(dǎo)航菜單實(shí)例代碼
- CSS3+Js實(shí)現(xiàn)響應(yīng)式導(dǎo)航條
- JS 實(shí)現(xiàn)導(dǎo)航欄懸停效果
- JavaScript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊導(dǎo)航欄變色特效
- js實(shí)現(xiàn)無限級(jí)樹形導(dǎo)航列表效果代碼
- js實(shí)現(xiàn)的常用的左側(cè)導(dǎo)航效果
- JavaScript實(shí)現(xiàn)滑動(dòng)導(dǎo)航欄效果
- 原生js實(shí)現(xiàn)波浪導(dǎo)航效果
相關(guān)文章
JavaScript手寫源碼之omit函數(shù)的實(shí)現(xiàn)
最近突然有個(gè)新的想法,想去看看前端的小庫(kù)來提升自己的編碼能力。但是又不知道怎么去證明自己是否真的看懂了,那就實(shí)現(xiàn)一個(gè)omit函數(shù)吧2023-02-02
使用hasOwnProperty時(shí)報(bào)錯(cuò)的解決方法
hasOwnProperty這個(gè)方法是用來查找一個(gè)對(duì)象是否有某個(gè)屬性,且查找的屬性必須是對(duì)象本身的一個(gè)成員,但是不會(huì)去查找對(duì)象的原型鏈,文中介紹了使用示例代碼及使用時(shí)可能會(huì)遇到的問題,對(duì)hasOwnProperty報(bào)錯(cuò)原因分析及解決方法感興趣的朋友一起看看吧2024-01-01
JavaScript Image對(duì)象實(shí)現(xiàn)原理實(shí)例解析
這篇文章主要介紹了JavaScript Image對(duì)象實(shí)現(xiàn)原理實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
JS實(shí)現(xiàn)去除數(shù)組中重復(fù)json的方法示例
這篇文章主要介紹了JS實(shí)現(xiàn)去除數(shù)組中重復(fù)json的方法,涉及javascript針對(duì)json數(shù)組數(shù)據(jù)的遍歷、判斷、存取等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
11個(gè)教程中不常被提及的JavaScript小技巧(推薦)
這篇文章主要介紹了11個(gè)教程中不常被提及的JavaScript小技巧,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

