这是我为解决我的问题而编写的 jQuery。
$(document).ready(function () {
$('#dnnMenu').mouseout(function () {
if ((viewportRight - myRight) >= '540') {
$('#dnnMenu .subLevelRight').css('left', '-270px');
}
else { $('#dnnMenu .subLevelRight').css('left', '270px'); }
});
});
$(document).ready(function () {
function HoverOver() {
$(this).addClass('hover');
}
function HoverOut() {
$(this).removeClass('hover');
}
var config = {
sensitivity: 2,
interval: 100,
over: HoverOver,
timeout: 100,
out: HoverOut
};
$("#dnnMenu .topLevel > li.haschild").hoverIntent(config);
$(".subLevel li.haschild").hover(HoverOver, HoverOut);
});
$(document).ready(function () {
$('#position1').mouseout(function () {
var myLeft = $("#position1").offset().left;
var myTop = $("#position1").offset().top;
var myRight = myLeft + $("#position1").outerWidth();
var myBottom = myTop + $("#position1").outerHeight();
var viewportRight = $(window).width() + $(window).scrollLeft();
var viewportBottom = $(window).height() + $(window).scrollTop();
if ((viewportRight - myRight) >= '400') {
$('#dnnMenu .subLevelRight').css('left', '270px');
}
else { $('#dnnMenu .subLevelRight').css('left', '-270px'); }
});
});
$(document).ready(function () {
$('#position2').mouseout(function () {
var myLeft = $("#position2").offset().left;
var myTop = $("#position2").offset().top;
var myRight = myLeft + $("#position2").outerWidth();
var myBottom = myTop + $("#position2").outerHeight();
var viewportRight = $(window).width() + $(window).scrollLeft();
var viewportBottom = $(window).height() + $(window).scrollTop();
if ((viewportRight - myRight) >= '400') {
$('#dnnMenu .subLevelRight').css('left', '270px');
}
else { $('#dnnMenu .subLevelRight').css('left', '-270px'); }
});
});
$(document).ready(function () {
$('#position3').mouseout(function () {
var myLeft = $("#position3").offset().left;
var myTop = $("#position3").offset().top;
var myRight = myLeft + $("#position3").outerWidth();
var myBottom = myTop + $("#position3").outerHeight();
var viewportRight = $(window).width() + $(window).scrollLeft();
var viewportBottom = $(window).height() + $(window).scrollTop();
if ((viewportRight - myRight) >= '400') {
$('#dnnMenu .subLevelRight').css('left', '270px');
}
else { $('#dnnMenu .subLevelRight').css('left', '-270px'); }
});
});
$(document).ready(function () {
$('#position4').mouseout(function () {
var myLeft = $("#position4").offset().left;
var myTop = $("#position4").offset().top;
var myRight = myLeft + $("#position4").outerWidth();
var myBottom = myTop + $("#position4").outerHeight();
var viewportRight = $(window).width() + $(window).scrollLeft();
var viewportBottom = $(window).height() + $(window).scrollTop();
if ((viewportRight - myRight) >= '400') {
$('#dnnMenu .subLevelRight').css('left', '270px');
}
else { $('#dnnMenu .subLevelRight').css('left', '-270px'); }
});
});
$(document).ready(function () {
$('#position5').mouseout(function () {
var myLeft = $("#position5").offset().left;
var myTop = $("#position5").offset().top;
var myRight = myLeft + $("#position5").outerWidth();
var myBottom = myTop + $("#position5").outerHeight();
var viewportRight = $(window).width() + $(window).scrollLeft();
var viewportBottom = $(window).height() + $(window).scrollTop();
if ((viewportRight - myRight) >= '400') {
$('#dnnMenu .subLevelRight').css('left', '270px');
}
else { $('#dnnMenu .subLevelRight').css('left', '-270px'); }
});
});
$(document).ready(function () {
$('#position6').mouseout(function () {
var myLeft = $("#position6").offset().left;
var myTop = $("#position6").offset().top;
var myRight = myLeft + $("#position6").outerWidth();
var myBottom = myTop + $("#position6").outerHeight();
var viewportRight = $(window).width() + $(window).scrollLeft();
var viewportBottom = $(window).height() + $(window).scrollTop();
if ((viewportRight - myRight) >= '400') {
$('#dnnMenu .subLevelRight').css('left', '270px');
}
else { $('#dnnMenu .subLevelRight').css('left', '-270px'); }
});
});
$(document).ready(function () {
$('#position7').mouseout(function () {
var myLeft = $("#position7").offset().left;
var myTop = $("#position7").offset().top;
var myRight = myLeft + $("#position7").outerWidth();
var myBottom = myTop + $("#position7").outerHeight();
var viewportRight = $(window).width() + $(window).scrollLeft();
var viewportBottom = $(window).height() + $(window).scrollTop();
if ((viewportRight - myRight) >= '400') {
$('#dnnMenu .subLevelRight').css('left', '270px');
}
else { $('#dnnMenu .subLevelRight').css('left', '-270px'); }
});
});
这是CSS
#position1{
background:transparent no-repeat;
width:110px;
height:2px;
margin-left:15px;
margin-top:-2px;
position:absolute;}
#position2{
background:transparent no-repeat;
width:118px;
height:2px;
margin-left:127px;
margin-top:-2px;
position:absolute;}
#position3{
background:transparent no-repeat;
width:153px;
height:2px;
margin-left:247px;
margin-top:-2px;
position:absolute;}
#position4{
background:transparent no-repeat;
width:178px;
height:2px;
margin-left:402px;
margin-top:-2px;
position:absolute;}
#position5{
background:transparent no-repeat;
width:213px;
height:2px;
margin-left:582px;
margin-top:-2px;
position:absolute;}
#position6{
background:transparent no-repeat;
width:106px;
height:2px;
margin-left:797px;
margin-top:-2px;
position:absolute;}
#position7{
background:transparent no-repeat;
width:78px;
height:2px;
margin-left:905px;
margin-top:-2px;
position:absolute;}
我唯一添加的另一件事是 div id 直接位于我在 HTML 中的其他菜单项下
<div id="position1"></div><div id="position2"></div><div id="position3"></div><div id="position4"></div><div id="position5"></div><div id="position6"></div><div id="position7"></div>
它只是不确定是否一切都写得很完美。如果您发现有问题,请告诉我,以便我清理代码。