jQuery:这是我编写的代码,但无法正常工作,我需要在单击菜单时使活动圈子保持活动链接,而不是回到顶部.
>当悬停导航链接圆圈点到达那里时
>单击导航链接时,圆点保持活动链接
>如果链接处于活动状态,并且在将鼠标悬停在其他任何链接上之后在此处添加圆点,则圆圈点将转到该位置并返回到活动链接
如果您有任何建议或解决方案,请给我答复
jQuery(document).ready(function() {
var activelenght = jQuery(".navigation_Box ul li.active a").length;
var activelenghtfirst = jQuery(".navigation_Box ul li#secmain.active a").length;
var moveobj = jQuery(".nav_active_dot");
var activeobj = jQuery(".navigation_Box ul li.active a");
var activeobjoff = activeobj.offset().top;
var activeparentoff = activeobj.parent().parent().offset().top;
var finaloffactive = activeobjoff-activeparentoff;
if (activelenghtfirst > 0) {
jQuery(moveobj).css("top",finaloffactive);
jQuery(moveobj).css("opacity",1);
} else {
jQuery(moveobj).css("opacity",0);
}
jQuery(".navigation_Box ul li a").each(function() {
jQuery(this).mouSEOver(function () {
var obj = jQuery(this);
var childPos = obj.offset();
var parentPos = obj.parent().parent().offset();
var childOffset = childPos.top - parentPos.top;
jQuery(moveobj).css("opacity",1);
jQuery(moveobj).css("top",childOffset);
});
jQuery(this).mouSEOut(function() {
if( activelenghtfirst > 0) {
jQuery(moveobj).css("top",0);
jQuery(moveobj).css("opacity",0);
console.log("2");
} else {
jQuery(moveobj).css("top",0);
}
});
jQuery(this).click(function(e) {
e.preventDefault();
jQuery(this).parent().addClass("active");
jQuery(this).parent().siblings().removeClass("active");
});
});
});
* { -webkit-Box-sizing:border-Box; -moz-Box-sizing:border-Box; Box-sizing:border-Box;}
.navigation_Box { font-family:arial; position:relative;}
.navigation_Box ul { display:block; margin:0; padding:0; position:relative;}
.navigation_Box ul li { display:block; margin:0; padding:0; position:relative;}
.navigation_Box ul li a { color:#000; margin:0; padding:7px 0 7px 30px; font-size:14px; display:inline-block; vertical-align:top; position:relative; text-decoration:none;}
.navigation_Box ul li a .circle_border { width:10px; height:10px; border-radius:50px; background:#fff; border:solid 1px #000; position:absolute; left:0; top:10px; z-index:1;}
.navigation_Box ul li.active a { color:#47c5f3;}
.navigation_Box ul li a:hover { color:#47c5f3;}
.navigation_Box ul li a:after { position:absolute; left:5px; width:1px; content:''; background:#000; height:30px; top:19px;}
.navigation_Box ul li:last-child a:after { display:none;}
.navigation_Box .nav_active_dot { position:absolute; left:0; top:0; width:10px; height:10px; background:#47c5f3; border-radius:20px; margin:10px 0 0 0; opacity:0; z-index:2;
-webkit-transition:all ease-in-out 0.3s;
-moz-transition:all ease-in-out 0.3s;
transition:all ease-in-out 0.3s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="navigation_Box">
<ul>
<li class="active"><a href="#" id="secmain" style="opacity:0; visibility:hidden;"><span class="circle_border"></span>Who we are</a></li>
<li><a href="#" id="secwho"><span class="circle_border"></span>Home</a></li>
<li><a href="#" id="secbes"><span class="circle_border"></span>Solutions</a></li>
<li><a href="#" id="secfac"><span class="circle_border"></span>Factor</a></li>
<li><a href="#" id="sectra"><span class="circle_border"></span>Stories</a></li>
<li><a href="#" id="secfor"><span class="circle_border"></span>Services</a></li>
<li><a href="#" id="secnew"><span class="circle_border"></span>News</a></li>
</ul>
<div class="nav_active_dot"></div>
</div>
最佳答案
您可以使用以下方法使活动的项目符号变为蓝色:
原文链接:https://www.f2er.com/html/530476.html.navigation_Box ul li.active a .circle_border {
background: #47c5f3;
border: 1px solid #47c5f3;
}
请参见下面的演示:
jQuery(document).ready(function() {
var activelenght = jQuery(".navigation_Box ul li.active a").length;
var activelenghtfirst = jQuery(".navigation_Box ul li#secmain.active a").length;
var moveobj = jQuery(".nav_active_dot");
var activeobj = jQuery(".navigation_Box ul li.active a");
var activeobjoff = activeobj.offset().top;
var activeparentoff = activeobj.parent().parent().offset().top;
var finaloffactive = activeobjoff - activeparentoff;
if (activelenghtfirst > 0) {
jQuery(moveobj).css("top",finaloffactive);
jQuery(moveobj).css("opacity",1);
} else {
jQuery(moveobj).css("opacity",0);
}
jQuery(".navigation_Box ul li a").each(function() {
jQuery(this).mouSEOver(function() {
var obj = jQuery(this);
var childPos = obj.offset();
var parentPos = obj.parent().parent().offset();
var childOffset = childPos.top - parentPos.top;
jQuery(moveobj).css("opacity",1);
jQuery(moveobj).css("top",childOffset);
});
jQuery(this).mouSEOut(function() {
if (activelenghtfirst > 0) {
jQuery(moveobj).css("top",0);
jQuery(moveobj).css("opacity",0);
console.log("2");
} else {
jQuery(moveobj).css("top",0);
}
});
jQuery(this).click(function(e) {
e.preventDefault();
jQuery(this).parent().addClass("active");
jQuery(this).parent().siblings().removeClass("active");
});
});
});
* {
-webkit-Box-sizing: border-Box;
-moz-Box-sizing: border-Box;
Box-sizing: border-Box;
}
.navigation_Box {
font-family: arial;
position: relative;
}
.navigation_Box ul {
display: block;
margin: 0;
padding: 0;
position: relative;
}
.navigation_Box ul li {
display: block;
margin: 0;
padding: 0;
position: relative;
}
.navigation_Box ul li a {
color: #000;
margin: 0;
padding: 7px 0 7px 30px;
font-size: 14px;
display: inline-block;
vertical-align: top;
position: relative;
text-decoration: none;
}
.navigation_Box ul li a .circle_border {
width: 10px;
height: 10px;
border-radius: 50px;
background: #fff;
border: solid 1px #000;
position: absolute;
left: 0;
top: 10px;
z-index: 1;
}
.navigation_Box ul li.active a {
color: #47c5f3;
}
.navigation_Box ul li.active a .circle_border { /* added */
background: #47c5f3;
border: 1px solid #47c5f3;
}
.navigation_Box ul li a:hover {
color: #47c5f3;
}
.navigation_Box ul li a:after {
position: absolute;
left: 5px;
width: 1px;
content: '';
background: #000;
height: 30px;
top: 19px;
}
.navigation_Box ul li:last-child a:after {
display: none;
}
.navigation_Box .nav_active_dot {
position: absolute;
left: 0;
top: 0;
width: 10px;
height: 10px;
background: #47c5f3;
border-radius: 20px;
margin: 10px 0 0 0;
opacity: 0;
z-index: 2;
-webkit-transition: all ease-in-out 0.3s;
-moz-transition: all ease-in-out 0.3s;
transition: all ease-in-out 0.3s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="navigation_Box">
<ul>
<li class="active"><a href="#" id="secmain" style="opacity:0; visibility:hidden;"><span class="circle_border"></span>Who we are</a></li>
<li><a href="#" id="secwho"><span class="circle_border"></span>Home</a></li>
<li><a href="#" id="secbes"><span class="circle_border"></span>Solutions</a></li>
<li><a href="#" id="secfac"><span class="circle_border"></span>Factor</a></li>
<li><a href="#" id="sectra"><span class="circle_border"></span>Stories</a></li>
<li><a href="#" id="secfor"><span class="circle_border"></span>Services</a></li>
<li><a href="#" id="secnew"><span class="circle_border"></span>News</a></li>
</ul>
<div class="nav_active_dot"></div>
</div>
但是你已经这样说了:
When link is active and circle dot there after hover any another link
the circle dot goes to there and go back to active link
在这种情况下,您可以使用变量(例如下面的演示中的homePos)来设置默认位置.您可以在鼠标移出时返回该默认位置,然后在活动链接上进行设置-参见下文:
jQuery(document).ready(function() {
var homePos = 0;
var activelenght = jQuery(".navigation_Box ul li.active a").length;
var activelenghtfirst = jQuery(".navigation_Box ul li#secmain.active a").length;
var moveobj = jQuery(".nav_active_dot");
var activeobj = jQuery(".navigation_Box ul li.active a");
var activeobjoff = activeobj.offset().top;
var activeparentoff = activeobj.parent().parent().offset().top;
var finaloffactive = activeobjoff - activeparentoff;
if (activelenghtfirst > 0) {
jQuery(moveobj).css("top",childOffset);
});
jQuery(this).mouSEOut(function() {
jQuery(moveobj).css("top",homePos); /* changed */
jQuery(moveobj).css("opacity",homePos); /* changed */
});
jQuery(this).click(function(e) {
e.preventDefault();
jQuery(this).parent().addClass("active");
jQuery(this).parent().siblings().removeClass("active");
/* added below */
var obj = jQuery(this);
var childPos = obj.offset();
var parentPos = obj.parent().parent().offset();
homePos = childPos.top - parentPos.top;
});
});
});
* {
-webkit-Box-sizing: border-Box;
-moz-Box-sizing: border-Box;
Box-sizing: border-Box;
}
.navigation_Box {
font-family: arial;
position: relative;
}
.navigation_Box ul {
display: block;
margin: 0;
padding: 0;
position: relative;
}
.navigation_Box ul li {
display: block;
margin: 0;
padding: 0;
position: relative;
}
.navigation_Box ul li a {
color: #000;
margin: 0;
padding: 7px 0 7px 30px;
font-size: 14px;
display: inline-block;
vertical-align: top;
position: relative;
text-decoration: none;
}
.navigation_Box ul li a .circle_border {
width: 10px;
height: 10px;
border-radius: 50px;
background: #fff;
border: solid 1px #000;
position: absolute;
left: 0;
top: 10px;
z-index: 1;
}
.navigation_Box ul li.active a {
color: #47c5f3;
}
.navigation_Box ul li a:hover {
color: #47c5f3;
}
.navigation_Box ul li a:after {
position: absolute;
left: 5px;
width: 1px;
content: '';
background: #000;
height: 30px;
top: 19px;
}
.navigation_Box ul li:last-child a:after {
display: none;
}
.navigation_Box .nav_active_dot {
position: absolute;
left: 0;
top: 0;
width: 10px;
height: 10px;
background: #47c5f3;
border-radius: 20px;
margin: 10px 0 0 0;
opacity: 0;
z-index: 2;
-webkit-transition: all ease-in-out 0.3s;
-moz-transition: all ease-in-out 0.3s;
transition: all ease-in-out 0.3s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="navigation_Box">
<ul>
<li class="active"><a href="#" id="secmain" style="opacity:0; visibility:hidden;"><span class="circle_border"></span>Who we are</a></li>
<li><a href="#" id="secwho"><span class="circle_border"></span>Home</a></li>
<li><a href="#" id="secbes"><span class="circle_border"></span>Solutions</a></li>
<li><a href="#" id="secfac"><span class="circle_border"></span>Factor</a></li>
<li><a href="#" id="sectra"><span class="circle_border"></span>Stories</a></li>
<li><a href="#" id="secfor"><span class="circle_border"></span>Services</a></li>
<li><a href="#" id="secnew"><span class="circle_border"></span>News</a></li>
</ul>
<div class="nav_active_dot"></div>
</div>