nav.js
1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var $ = require('yoho-jquery'),
IScroll = require('yoho-iscroll');
// nav 滚动
function initNavScroll(opt) {
var $navBox,
iScroll,
_default = {
el: '.outlet-nav'
},
options;
options = $.extend({}, _default, opt);
$navBox = $(options.el);
function scroll(index) {
var $ele;
$ele = $navBox.find('li').eq(index);
if ($ele.length > 0) {
setTimeout(function() {
iScroll.scrollToElement($ele[0], 400);
}, 1);
}
}
iScroll = new IScroll($navBox[0], {
scrollX: true,
scrollY: false
});
return {
goto: scroll
};
}
//获取url中的参数
function getUrlParam(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)'); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
//返回参数值
if (r !== null) {
return decodeURIComponent(r[2]);
} else {
return null;
}
}
//选中顶部导航
function activeNav() {
var $nav = $('#index_nav');
var index = getUrlParam('yh_channel');
if (index === null) {
index = 0;
}
$nav.find('li').eq(index).addClass('active').siblings().removeClass('active');
}
activeNav();
module.exports = initNavScroll;