导航元素的滑动背景只适用于2个元素,不能更多
Sliding background for navigation elements works only with 2 elements, not more
我的页面上有一个响应式页面选择器。我正在尝试为当前活动的元素创建一个彩色背景。目前的代码工作良好的2个元素,但如果我添加更多,那么定位将无法工作。这可能与我对.index()
jQuery函数缺乏了解有关。
下面是控制背景位置的JavaScript代码:($.parseURL()
函数只是测试页面的URL,然后这段代码将其与给定的列表元素匹配)
$.pageSlider = function(todo){
if (todo === 'update'){
$('.pageSlider a.active:not([href="'+$.parseURL().hash+'"])').removeClass('active');
$('.pageSlider a[href="'+$.parseURL().hash+'"]').addClass('active');
$('.pageSlider').each(function(){
var $this = $(this);
var $slideBtn = $this.find('a.slide-button');
var $navs = $this.find('a[href]');
var navsSize = $navs.size();
var percent = 100/navsSize;
var actNavIndx = $navs.index('a.active');
$navs.css('width',percent+'%');
console.log(actNavIndx);
$slideBtn.css('width',percent+'%').animate({left:((actNavIndx*-1)*percent)+'%'});
});
return $('.pageSlider a.active[href="'+$.parseURL('/index.php').hash+'"]');
}
};
window.location.hash = 'lounge';
$.pageSlider("update");
$(window).on('hashchange',function(){
$.pageSlider("update");
});
2元素演示:
(function($) {
$.parseURL = function(str) {
if (typeof str === 'undefined' && typeof window.location.href !== 'undefined') str = window.location.href;
var check = {
whole: /^((http|https):'/'/)?((.*'.)?.*'..*|localhost|'d{1,3}'.'d{1,3}'.'d{1,3}'.'d{1,3})'/.*/,
protocol: /^(http|https):'/'//,
absolute: /^'/'/((.*'.)?.*'..*|localhost|'d{1,3}'.'d{1,3}'.'d{1,3}'.'d{1,3})'//,
relative: /^['/](?!'/).*('..*|'/)?.*$/,
params: /'?.*=.*('&.*'=.*)+[^#.*]/,
},
r = {
protocol: undefined,
hostname: undefined,
hash: undefined,
href: {
original: undefined,
noparams: undefined,
},
parentDomain: undefined,
parameters: undefined,
pathname: {
nohash: undefined,
withhash: undefined,
},
type: undefined,
};
if (typeof str !== 'string') throw new TypeError('Argument must be a string!');
else if (!check.whole.test(str) && !check.absolute.test(str) && !check.relative.test(str)) throw new Error('Invalid string');
r.href.original = str;
if (str.indexOf('#') !== -1) {
r.hash = str.substring(str.indexOf('#'));
str = str.substring(0, str.indexOf('#'));
}
if (check.params.test(str)) {
r.parameters = {};
var urlparams = str.match(check.params)[0].substring(1).split("&");
for (var i = 0, l = urlparams.length; i < l; i++) {
var item = urlparams[i].split('=');
r.parameters[item[0]] = item[1];
}
str = str.replace(check.params, '');
r.href.noparams = str + (typeof r.hash !== 'undefined' ? r.hash : '');
}
if (check.protocol.test(str)) {
r.protocol = str.match(check.protocol)[0];
r.hostname = str.replace(check.protocol, '');
r.hostname = r.hostname.substring(0, r.hostname.indexOf('/'));
r.parentDomain = r.hostname.split('.');
r.parentDomain.splice(0, 1);
r.parentDomain = r.parentDomain.join('.');
if (r.parentDomain == '') r.parentDomain = r.hostname;
var tmp = str.replace(check.protocol, '');
r.pathname.nohash = tmp.substring(tmp.indexOf('/'));
tmp = undefined;
r.type = 'normal';
} else {
r.protocol = window.location.href.match(check.protocol)[0];
if (check.absolute.test(str)) { //Link starts with "//"
var tmp = str.replace(/^'/'//, '');
r.hostname = tmp.substring(0, tmp.indexOf('/'));
r.pathnam.nohashe = tmp.substring(tmp.indexOf('/'));
r.type = 'absolute';
r.href.original = r.href.original.replace(/^'/'//g, r.protocol);
} else if (check.relative.test(str)) {
r.hostname = window.location.href.replace(check.protocol, '');
r.hostname = r.hostname.substring(0, r.hostname.indexOf('/'));
r.pathname.nohash = r.href.original;
r.type = 'relative';
} else {
throw new Error('Invalid string');
}
}
r.pathname.withhash = r.pathname.nohash + r.hash;
return r;
};
if (typeof $('.pageSlider')[0] !== 'undefined') {
$.pageSlider = function(todo) {
if (todo === 'update') {
$('.pageSlider a.active:not([href="' + $.parseURL().hash + '"])').removeClass('active');
$('.pageSlider a[href="' + $.parseURL().hash + '"]').addClass('active');
$('.pageSlider').each(function() {
var $this = $(this);
var $slideBtn = $this.find('a.slide-button');
var $navs = $this.find('a[href]');
var navsSize = $navs.size();
var percent = 100 / navsSize;
var actNavIndx = $navs.index('a.active');
$navs.css('width', percent + '%');
console.log(actNavIndx);
$slideBtn.css('width', percent + '%').animate({
left: ((actNavIndx * -1) * percent) + '%'
});
});
return $('.pageSlider a.active[href="' + $.parseURL('/index.php').hash + '"]');
}
};
window.location.hash = 'lounge';
$.pageSlider("update");
$(window).on('hashchange', function() {
$.pageSlider("update");
});
}
})(jQuery);
body {
background-color: #777;
}
@media all and (max-width: 500px) {
.pageSlider {
white-space: normal;
}
.pageSlider a[href] {
display: block !important;
width: 100% !important;
}
.pageSlider a[href].active {
background-color: #cccc00;
}
.pageSlider .slide-button {
display: none;
}
}
.pageSlider {
margin: 15px auto !important;
font-size: 1.2em;
white-space: nowrap;
height: 35px;
}
.pageSlider a[href] {
display: inline-block;
text-align: center;
text-decoration: none;
color: #fff;
position: relative;
z-index: 2;
}
.pageSlider a[href].active {
color: #000;
}
.pageSlider .slide-button {
background-color: #cccc00;
position: relative;
top: -35px;
display: inline-block;
padding: 0;
float: left;
height: inherit;
z-index: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="pageSlider">
<a href="#lounge" class="active">Visitor's Lounge</a>
<a href="#streams">News Stream</a>
<a class="slide-button"></a>
</div>
vs。3元素demo
(function($){
$.parseURL = function(str) {
if (typeof str === 'undefined' && typeof window.location.href !== 'undefined') str = window.location.href;
var check = {
whole: /^((http|https):'/'/)?((.*'.)?.*'..*|localhost|'d{1,3}'.'d{1,3}'.'d{1,3}'.'d{1,3})'/.*/,
protocol: /^(http|https):'/'//,
absolute: /^'/'/((.*'.)?.*'..*|localhost|'d{1,3}'.'d{1,3}'.'d{1,3}'.'d{1,3})'//,
relative: /^['/](?!'/).*('..*|'/)?.*$/,
params: /'?.*=.*('&.*'=.*)+[^#.*]/,
},
r = {
protocol: undefined,
hostname: undefined,
hash: undefined,
href: {
original: undefined,
noparams: undefined,
},
parentDomain: undefined,
parameters: undefined,
pathname: {
nohash: undefined,
withhash: undefined,
},
type: undefined,
};
if (typeof str !== 'string') throw new TypeError('Argument must be a string!');
else if (!check.whole.test(str) && !check.absolute.test(str) && !check.relative.test(str)) throw new Error('Invalid string');
r.href.original = str;
if (str.indexOf('#') !== -1){
r.hash = str.substring(str.indexOf('#'));
str = str.substring(0,str.indexOf('#'));
}
if (check.params.test(str)){
r.parameters = {};
var urlparams = str.match(check.params)[0].substring(1).split("&");
for (var i=0,l=urlparams.length; i<l; i++){
var item = urlparams[i].split('=');
r.parameters[item[0]] = item[1];
}
str = str.replace(check.params,'');
r.href.noparams = str+(typeof r.hash !== 'undefined' ? r.hash : '');
}
if (check.protocol.test(str)){
r.protocol = str.match(check.protocol)[0];
r.hostname = str.replace(check.protocol,'');
r.hostname = r.hostname.substring(0,r.hostname.indexOf('/'));
r.parentDomain = r.hostname.split('.');
r.parentDomain.splice(0,1);
r.parentDomain = r.parentDomain.join('.');
if (r.parentDomain == '') r.parentDomain = r.hostname;
var tmp = str.replace(check.protocol,'');
r.pathname.nohash = tmp.substring(tmp.indexOf('/'));
tmp = undefined;
r.type = 'normal';
}
else {
r.protocol = window.location.href.match(check.protocol)[0];
if (check.absolute.test(str)){ //Link starts with "//"
var tmp = str.replace(/^'/'//,'');
r.hostname = tmp.substring(0,tmp.indexOf('/'));
r.pathnam.nohashe = tmp.substring(tmp.indexOf('/'));
r.type = 'absolute';
r.href.original = r.href.original.replace(/^'/'//g,r.protocol);
}
else if (check.relative.test(str)){
r.hostname = window.location.href.replace(check.protocol,'');
r.hostname = r.hostname.substring(0,r.hostname.indexOf('/'));
r.pathname.nohash = r.href.original;
r.type = 'relative';
}
else {
throw new Error('Invalid string');
}
}
r.pathname.withhash = r.pathname.nohash+r.hash;
return r;
};
if (typeof $('.pageSlider')[0] !== 'undefined'){
$.pageSlider = function(todo){
if (todo === 'update'){
$('.pageSlider a.active:not([href="'+$.parseURL().hash+'"])').removeClass('active');
$('.pageSlider a[href="'+$.parseURL().hash+'"]').addClass('active');
$('.pageSlider').each(function(){
var $this = $(this);
var $slideBtn = $this.find('a.slide-button');
var $navs = $this.find('a[href]');
var navsSize = $navs.size();
var percent = 100/navsSize;
var actNavIndx = $navs.index('a.active');
$navs.css('width',percent+'%');
console.log(actNavIndx);
$slideBtn.css('width',percent+'%').animate({left:((actNavIndx*-1)*percent)+'%'});
});
return $('.pageSlider a.active[href="'+$.parseURL('/index.php').hash+'"]');
}
};
window.location.hash = 'lounge';
$.pageSlider("update");
$(window).on('hashchange',function(){
$.pageSlider("update");
});
}
})(jQuery);
body {
background-color:#777;
}
@media all and (max-width: 500px){
.pageSlider {
white-space: normal;
}
.pageSlider a[href]{
display: block !important;
width: 100% !important;
}
.pageSlider a[href].active {
background-color: #cccc00;
}
.pageSlider .slide-button {
display: none;
}
}
.pageSlider {
margin: 15px auto !important;
font-size: 1.2em;
white-space: nowrap;
height: 35px;
}
.pageSlider a[href] {
display: inline-block;
text-align: center;
text-decoration: none;
color: #fff;
position: relative;
z-index: 2;
}
.pageSlider a[href].active {
color: #000;
}
.pageSlider .slide-button {
background-color: #cccc00;
position: relative;
top: -35px;
display: inline-block;
padding: 0;
float: left;
height: inherit;
z-index: 1;
}
<div class="pageSlider">
<a href="#lounge" class="active">Visitor's Lounge</a>
<a href="#streams">News Stream</a>
<a href="#test">Test</a>
<a class="slide-button"></a>
</div>
你的index()确实是误用了:
var actNavIndx = $navs.index('a.active');
应为:
var actNavIndx = $this.children('a.active').index();
和动画:
$slideBtn.css('width',percent+'%').animate({left:((actNavIndx*-1)*percent)+'%'});
应为:
$slideBtn.css('width',percent+'%').animate({left:((actNavIndx)*percent)+'%'});
相关文章:
- 使用数据上的角度更改设置集合的第一个元素的动画
- 在数组中的一个元素上设置多个值
- Rails/Javascript链接到用于切换多个元素的函数
- 两个指令创建新的继承的和隔离的作用域-元素得到哪个
- 如何使用jquery animate来回移动多个元素
- 如何在jQuery中包装两个元素的组
- JavaScript:当有两个B类元素时,如何在id X中的B类中选择A类
- 在页面上创建多个元素,而不是一个发生更改的元素
- Javascript获取具有不同id的文件数's来自没有多个属性的文件输入元素
- 一个页面上有多个Ace编辑器,没有预先设置元素
- 使用filter和map方法将数组中某些元素的第一个字母大写-JavaScript
- 无法使以下ng on click outside指令在同一页面上的两个元素上工作
- 在多个元素上使用jquery插件,只需稍作修改
- CSS3:当页面向下滚动到第一个视口时,是否可以在元素上触发转换
- jQuery body onChange具有多个元素和多个函数
- jQuery slice() 方法 在 html 中选择元素(第一个/最后一个)
- 获取元素多个ID或CLASS
- 在选择元素填充后立即执行带有选择元素第一个选项的Javascript函数
- 使用JQuery在动态生成的html表中获取td元素'5个索引
- 如何在对象中停止和重置几个动画元素?(几个独立的动画)