我获得了一个切换控件,可在单击时在菜单图标和X
之间进行动画显示。它还提供了叠加导航。
问题是:当您点击叠加导航中的任何链接时,它不会将X
更改/切换回默认菜单图标。
在下面的代码片段中,我添加了$('.cd-menu-icon').toggleClass('is-clicked'); $('.cd-header').toggleClass('menu-is-open');
(第33,34行),以初步设置控件的切换状态。
jQuery(document).ready(function($){
//if you change this breakpoint in the style.css file (or _layout.scss if you use SASS), don't forget to update this value as well
var MQL = 1170;
//primary navigation slide-in effect
if($(window).width() > MQL) {
var headerHeight = $('.cd-header').height();
$(window).on('scroll',
{
previousTop: 0
}, function () {
var currentTop = $(window).scrollTop();
//check if user is scrolling up
if (currentTop < this.previousTop ) {
//if scrolling up...
if (currentTop > 0 && $('.cd-header').hasClass('is-fixed')) {
$('.cd-header').addClass('is-visible');
} else {
$('.cd-header').removeClass('is-visible is-fixed');
}
} else {
//if scrolling down...
$('.cd-header').removeClass('is-visible');
if( currentTop > headerHeight && !$('.cd-header').hasClass('is-fixed')) $('.cd-header').addClass('is-fixed');
}
this.previousTop = currentTop;
});
}
//open/close primary navigation
$('.cd-primary-nav-trigger').on('click', function(){
$('.cd-menu-icon').toggleClass('is-clicked');
$('.cd-header').toggleClass('menu-is-open');
//in firefox transitions break when parent overflow is changed, so we need to wait for the end of the trasition to give the body an overflow hidden
if( $('.cd-primary-nav').hasClass('is-visible') ) {
$('.cd-primary-nav').removeClass('is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',function(){
$('body').removeClass('overflow-hidden');
});
} else {
$('.cd-primary-nav').addClass('is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',function(){
$('body').addClass('overflow-hidden');
});
}
});
});
希望有人可以提供帮助。
要重现:(使用 JsFiddle Example )
答案 0 :(得分:4)
我认为您已在HTML部分的1, 2, 3, 4
代码(第253行)中的<script>
个链接上实施了自定义点击事件。
只需将<script>
标记替换为以下内容:
<script>
$('.cd-primary-nav a').on('click', function(){
$('.cd-menu-icon').removeClass('is-clicked');
$('.cd-header').removeClass('menu-is-open');
$('.cd-primary-nav').removeClass('is-visible').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend',function(){
$('body').removeClass('overflow-hidden');
});
});
</script>
JSFiddle:https://jsfiddle.net/c9dj010v/
答案 1 :(得分:-1)
快速解决方案是添加一个新的点击监听器,用于收听链接点击
print value1 is value2
在您的原始小提琴中,汉堡包图标只会在直接点击时更改,因为点击监听器仅附加到该元素