navbarscroll.js 5.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * 移动端模拟导航可点击自动滑动 0.1.4
  3. * Date: 2017-01-11
  4. * by: xiewei
  5. * 导航可左右滑动,可点击边缘的一个,自动滚动下一个到可视范围【依赖于iscroll.js】
  6. */
  7. (function ($) {
  8. $.fn.navbarscroll = function (options) {
  9. //各种属性、参数
  10. var _defaults = {
  11. className:'cur', //当前选中点击元素的class类名
  12. clickScrollTime:300, //点击后滑动时间
  13. duibiScreenWidth:0.4, //单位以rem为准,默认为0.4rem
  14. scrollerWidth:3, //单位以px为准,默认为3,[仅用于特殊情况:外层宽度因为小数点造成的不精准情况]
  15. defaultSelect:0, //初始选中第n个,默认第0个
  16. fingerClick:0, //目标第0或1个选项触发,必须每一项长度一致,方可用此项
  17. oid:'numberID',
  18. endClickScroll:function(thisObj){}//回调函数
  19. }
  20. var _opt = $.extend(_defaults, options);
  21. this.each(function () {
  22. //插件实现代码
  23. var _wrapper = $(this);
  24. var _win = $(window);
  25. var _win_width = _win.width(),_wrapper_width = _wrapper.width(),_wrapper_off_left = _wrapper.offset().left;
  26. var _wrapper_off_right=_win_width-_wrapper_off_left-_wrapper_width;
  27. var _obj_scroller = _wrapper.children('.scroller');
  28. var _obj_ul = _obj_scroller.children('ul');
  29. var _obj_li = _obj_ul.children('li');
  30. var _scroller_w = 0;
  31. _obj_li.css({"margin-left":"0","margin-right":"0"});
  32. for (var i = 0; i < _obj_li.length; i++) {
  33. _scroller_w += _obj_li[i].offsetWidth;
  34. }
  35. _obj_scroller.width(_scroller_w+_opt.scrollerWidth);
  36. var myScroll = new IScroll('#'+_wrapper.attr('id'), {
  37. eventPassthrough: true,
  38. scrollX: true,
  39. scrollY: false,
  40. preventDefault: false
  41. });
  42. _init(_obj_li.eq(_opt.defaultSelect));
  43. _obj_li.click(function(){
  44. _init($(this));
  45. });
  46. //解决PC端谷歌浏览器模拟的手机屏幕出现莫名的卡顿现象,滑动时禁止默认事件(2017-01-11)
  47. _wrapper[0].addEventListener('touchmove',function (e){e.preventDefault();},false);
  48. function _init(thiObj){
  49. var $this_obj=thiObj;
  50. 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();
  51. var this_off_right=_win_width-this_off_left-this_width;
  52. if(_scroller_w+2>_wrapper_width){
  53. if(_opt.fingerClick==1){
  54. if(this_index==1){
  55. myScroll.scrollTo(-this_pos_left+this_prev_width,0, _opt.clickScrollTime);
  56. }else if(this_index==0){
  57. myScroll.scrollTo(-this_pos_left,0, _opt.clickScrollTime);
  58. }else if(this_index==_obj_li.length-2){
  59. myScroll.scrollBy(this_off_right-_wrapper_off_right-this_width,0, _opt.clickScrollTime);
  60. }else if(this_index==_obj_li.length-1){
  61. myScroll.scrollBy(this_off_right-_wrapper_off_right,0, _opt.clickScrollTime);
  62. }else{
  63. if(this_off_left-_wrapper_off_left-(this_width*_opt.fingerClick)<duibi){
  64. myScroll.scrollTo(-this_pos_left+this_prev_width+(this_width*_opt.fingerClick),0, _opt.clickScrollTime);
  65. }else if(this_off_right-_wrapper_off_right-(this_width*_opt.fingerClick)<duibi){
  66. myScroll.scrollBy(this_off_right-this_next_width-_wrapper_off_right-(this_width*_opt.fingerClick),0, _opt.clickScrollTime);
  67. }
  68. }
  69. }else{
  70. if(this_index==1){
  71. myScroll.scrollTo(-this_pos_left+this_prev_width,0, _opt.clickScrollTime);
  72. }else if(this_index==_obj_li.length-1){
  73. if(this_off_right-_wrapper_off_right>1||this_off_right-_wrapper_off_right<-1){
  74. myScroll.scrollBy(this_off_right-_wrapper_off_right,0, _opt.clickScrollTime);
  75. }
  76. }else{
  77. if(this_off_left-_wrapper_off_left<duibi){
  78. myScroll.scrollTo(-this_pos_left+this_prev_width,0, _opt.clickScrollTime);
  79. }else if(this_off_right-_wrapper_off_right<duibi){
  80. myScroll.scrollBy(this_off_right-this_next_width-_wrapper_off_right,0, _opt.clickScrollTime);
  81. }
  82. }
  83. }
  84. }
  85. $this_obj.addClass(_opt.className).siblings('li').removeClass(_opt.className);
  86. _opt.endClickScroll.call(this,$this_obj);
  87. }
  88. });
  89. };
  90. })(jQuery);