我想为我的菜单制作一个滑块。它应该高于实际活动的menuitem。 我无法在codepen中显示我的问题。但我可以展示我的jquery:
https://codepen.io/Ayalann/pen/xrrjgg
我从codepen获得了这段代码
$("ul li").click(function(e) {
// make sure we cannot click the slider
if ($(this).hasClass('slider')) {
return;
}
/* Add the slider movement */
// what tab was pressed
var whatTab = $(this).index();
// Work out how far the slider needs to go
var howFar = 160 * whatTab;
$(".slider").css({
left: howFar + "px"
});
/* Add the ripple */
// Remove olds ones
$(".ripple").remove();
// Setup
var posX = $(this).offset().left,
posY = $(this).offset().top,
buttonWidth = $(this).width(),
buttonHeight = $(this).height();
// Add the element
$(this).prepend("<span class='ripple'></span>");
// Make it round!
if (buttonWidth >= buttonHeight) {
buttonHeight = buttonWidth;
} else {
buttonWidth = buttonHeight;
}
// Get the center of the element
var x = e.pageX - posX - buttonWidth / 2;
var y = e.pageY - posY - buttonHeight / 2;
// Add the ripples CSS and start the animation
$(".ripple").css({
width: buttonWidth,
height: buttonHeight,
top: y + 'px',
left: x + 'px'
}).addClass("rippleEffect");
});
@import url(https://fonts.googleapis.com/css?family=Lato:100,300,400,700);
* {
box-sizing: border-box;
}
body {
font-family: 'Lato', sans-serif;
background: #222;
}
ul {
font-size: 0;
position: relative;
padding: 0;
width: 480px;
margin: 40px auto;
user-select: none;
}
li {
display: inline-block;
width: 160px;
height: 60px;
background: #39CCCC;
font-size: 16px;
text-align: center;
line-height: 60px;
color: #fff;
text-transform: uppercase;
position: relative;
overflow: hidden;
cursor: pointer;
}
.slider {
display: block;
position: absolute;
bottom: 0;
left: 0;
height: 4px;
background: yellow;
transition: all 0.5s;
}
/* Ripple */
.ripple {
width: 0;
height: 0;
border-radius: 50%;
background: rgba(255, 255, 255, 0.4);
transform: scale(0);
position: absolute;
opacity: 1;
}
.rippleEffect {
animation: rippleDrop .6s linear;
}
@keyframes rippleDrop {
100% {
transform: scale(2);
opacity: 0;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li>Item One</li>
<li>Item Two</li>
<li>Item Three</li>
<li class="slider"></li>
</ul>
我的问题是,当我点击页面中的菜单项时,滑块正在移动,但是当下载新页面时,滑块将转到开始。它不会保持在活动菜单项之上。
这就是我试图这样做的原因:
if ($(".sf-main-menu li").hasClass("active-trail")) {
$(".slider").css({
left: howFar + "px"
});
但它不起作用。
我该如何解决这个问题?
答案 0 :(得分:1)
创建一个函数并传递其中的活动li以进行定位滑块,并在单击列表时调用相同的函数,
$(function() {
function menuChange(ths) {
// make sure we cannot click the slider
/* if ($(this).hasClass('slider')) {
return;
}*/
/* Add the slider movement */
// what tab was pressed
var whatTab = $(ths).index();
// Work out how far the slider needs to go
var howFar = 100 * whatTab;
/* $(".slider").css({
left: howFar + "px"
});*/
if ($(".sf-main-menu li").hasClass("active-trail")) {
$(".slider").css({
left: howFar + "px"
});
}
}
$(".sf-main-menu li").click(function() {
menuChange(this);
});
menuChange($(".sf-main-menu li.active-trail"));
$('a').on('click',function(e){
e.preventDefault();
});
});
&#13;
.slider {
border-top: 3px solid green;
height: 10px;
width: 100px;
opacity: .8;
display: inline-block;
left: 50px;
transition: all 0.5s;
position: absolute;
}
.sf-main-menu li {
position: relative;
}
.sf-menu .active {
color: green;
}
.sf-menu li {
list-style-type: none;
width: 100px;
float: left;
padding-top: 10px;
}
.sf-menu a {
padding: 0 .5em;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="superfish-1" class="menu sf-menu sf-main-menu sf-horizontal sf-style-none sf-total-items-4 sf-parent-items-0 sf-single-items-4 superfish-processed sf-js-enabled">
<li id="menu-2025-1" class="first odd sf-item-1 sf-depth-1 sf-no-children"><a href="/fooldal" title="" class="sf-depth-1">Főoldal</a></li>
<li id="menu-396-1" class="active-trail middle even sf-item-2 sf-depth-1 sf-no-children"><a href="/hirek" class="sf-depth-1 active">Hírek</a></li>
<li id="menu-399-1" class="middle odd sf-item-3 sf-depth-1 sf-no-children"><a href="/magunkrol" class="sf-depth-1">Magunkról</a></li>
<li id="menu-400-1" class="last even sf-item-4 sf-depth-1 sf-no-children"><a href="/kapcsolat" class="sf-depth-1">Kapcsolat</a></li>
</ul>
<div class="slider"></div>
&#13;