我有一个函数会触发mouseenter(一个叠加层)上的事件,随后我将其解除绑定。在该函数中,有一个x我想关闭叠加层并允许用户重新触发该事件。现在,它无法与unbind(我需要的)一起使用,即使我从代码中取出unbind事件,css格式也已关闭。
这是我的功能:
$(function() {
$(document).mouseenter(function(event){
event.stopPropagation();
var docHeight = $(document).height() - ($(document).height() * .1);
if(event.pageY > docHeight){
$("#overlay").show();
displayOverlay();
displayModal(itemCount, imageArray, cartTotal);
$(this).unbind('mouseenter mouseleave');
}
$("#x").click(function(){
$("#overlay").hide();
});
});
});
这是我的CSS:
function displayOverlay() {
$("<div id='overlay'></div>").css({
"position": "fixed",
"top": "0px",
"height": "1200px",
"width": "100%",
"z-index": 100,
"background-color": "rgba(0,0,0,0.5)"
}).appendTo("body");
}
function displayModal(itemCount, imageArray, cartTotal) {
var imageHTML = "";
for (i = 0; i < imageArray.length; i++){
imageHTML += imageArray[i];
}
if (imageHTML === ""){
imageHTML = "You do not have any items in your cart";
}
$("<article id='modal'><div id='itemCount'>" + "Total Items: "
+ itemCount + "<div id='images'>" + imageHTML +
"<div id='cartTotal'>" + "Subtotal $" + cartTotal + "<div id='x'>"
+ "(X)" + "</div></div></div></div></article>").css({
"width": "461px",
"height": "263px",
"line-height": "200px",
"position": "fixed",
"top": "50%",
"left": "50%",
"margin-top": "-155px",
"margin-left": "-232px",
"background-color": "rgb(255,255,255)",
"border-radius": "5px",
"text-align": "center",
"z-index": 101,
"border": "1px solid rgb(227,227,227)",
"border-top": "4px solid rgb(217,0,31)"
}).appendTo("#overlay");
$("#itemCount").css({
"position": "relative",
"bottom": "79px",
"font-family": "Helvetica",
"color": "#000",
"font-size": "20px"
}).appendTo("#modal");
$("#images").css({
"position": "relative",
"bottom": "87px"
}).appendTo("#itemCount");
$("#cartTotal").css({
"position": "relative",
"bottom": "182px",
"color": "gray",
"font-size": "18px"
}).appendTo("#images");
$(".buttons").css({
"line-height": "10px",
"position": "relative",
"bottom":"86px"
}).appendTo("#cartTotal");
$("#x").css({
"position": "relative",
"top": "9px",
"font-size": "16px"
}).appendTo(".buttons");
$("a").css({
"color": "white"
});
}
最佳答案
代替使用$(this),尝试指定实际的元素(或选择器)名称。
$(document).unbind('mouseenter').unbind('mouseleave');
另外,可能是因为您要在一个unbind()函数中指定两个事件。
关于javascript - 在函数中使用Toggle类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33325475/