-1
以前我收到了一個標題,上面有一個顯示在懸停上的下拉菜單。 我想將其更改爲onclick。Dropdown onclick,點擊關閉
下拉菜單當前顯示onclick,但再次單擊時,下拉菜單不會關閉。只有當鼠標離開div時它纔會關閉。
我該如何改變這一點,以便下拉菜單總是打開,只有通過點擊網站上的某個地方或事件才能關閉下拉菜單。
代碼:
<a href="#header-cart" class="skip-link skip-cart <?php if($_cartQty <= 0): ?> no-count<?php endif; ?>">
<span class="icon"></span>
<span class="label"><?php echo $this->__('Cart'); ?></span>
<span class="count"><?php echo $_cartQty; ?></span>
</a>
<div id="header-cart" class="block block-cart skip-content">
<div class="block block-progress opc-block-progress minicart-wrapper">
<div class="block-title-cartsidebar">SHOPPINGCART</div>
<p class="empty"><?php echo $this->__('You have no items in your shopping cart.') ?></p>
</div>
</div>
</div>
我用這個代碼,網站上的下拉菜單:
<?php //Drop-down ?>
var ddOpenTimeout;
var dMenuPosTimeout;
var DD_DELAY_IN = 200;
var DD_DELAY_OUT = 0;
var DD_ANIMATION_IN = 0;
var DD_ANIMATION_OUT = 0;
$('.clickable-dropdown > .dropdown-heading').click(function() {
$(this).parent().addClass('open');
$(this).parent().trigger('mouseenter');
});
//$('.dropdown-heading').on('click', function(e) {
$(document).on('click', '.dropdown-heading', function(e) {
e.preventDefault();
});
$(document).on('click', '.dropdown', function() {
var ddToggle = $(this).children('.dropdown-heading');
var ddMenu = $(this).children('.dropdown-content');
var ddWrapper = ddMenu.parent(); <?php //$(this); ?>
<?php //Clear old position of dd menu ?>
ddMenu.css("left", "");
ddMenu.css("right", "");
<?php //Show dd menu ?>
if ($(this).hasClass('clickable-dropdown'))
{
<?php //If dropdown is opened (parent already has class 'open') ?>
if ($(this).hasClass('open'))
{
$(this).children('.dropdown-content').stop(true, true).delay(DD_DELAY_IN).fadeIn(DD_ANIMATION_IN, "easeOutCubic");
}
}
else
{
<?php //Add class 'open' to dd ?>
clearTimeout(ddOpenTimeout);
ddOpenTimeout = setTimeout(function() {
ddWrapper.addClass('open');
}, DD_DELAY_IN);
//$(this).addClass('open');
$(this).children('.dropdown-content').stop(true, true).delay(DD_DELAY_IN).fadeIn(DD_ANIMATION_IN, "easeOutCubic");
}
<?php //Set new position of dd menu.
//This code is delayed the same amount of time as drop-down animation. ?>
clearTimeout(dMenuPosTimeout);
dMenuPosTimeout = setTimeout(function() {
if (ddMenu.offset().left < 0)
{
var space = ddWrapper.offset().left; <?php //Space available on the left of dd ?>
ddMenu.css("left", (-1)*space);
ddMenu.css("right", "auto");
}
}, DD_DELAY_IN);
}).on('mouseleave', '.dropdown', function() {
var ddMenu = $(this).children('.dropdown-content');
clearTimeout(ddOpenTimeout); <?php //Clear, to close dd on mouseleave ?>
ddMenu.stop(true, true).delay(DD_DELAY_OUT).fadeOut(DD_ANIMATION_OUT, "easeInCubic");
if (ddMenu.is(":hidden"))
{
ddMenu.hide();
}
$(this).removeClass('open');
});
你願意私自爲你的php代碼生成html嗎?我無法在代碼中看到任何DropDown-Menu的HTML! –
@ Rami.Q我編輯代碼。 –
在jsfiddle中的演示將受到歡迎! :) –