我正在尝试实现jquery图像悬停弹出,但当弹出窗口更接近浏览器边缘时,它会遇到问题,当它发现空间不足以显示弹出窗口时,我希望它改变方向,我在许多插件中都看到了这种效果,弹出窗口、工具提示和下拉菜单如果接近浏览器窗口边缘,就会改变方向。
有谁能引导我朝正确的方向前进吗?,这是供参考的屏幕截图, http://img512.imageshack.us/img512/4990/browseredge.png
这里是jquery悬停代码
function imagePreview(){
/* CONFIG */
xOffset = 10;
yOffset = 30;
// these 2 variable determine popup's distance from the cursor
// you might want to adjust to get the right result
/* END CONFIG */
$("a.preview").hover(function(e){
this.t = this.title;
this.title = "";
var c = (this.t != "") ? "<br>" + this.t : "";
var newName = this.name;
//console.log(this.name);
newName=newName.replace("/l/","/o/");
//console.log(newName);
$("body").append("<p id='preview'><img src='"+ this.name +"' alt='Image preview' style='margin-bottom:5px;'>"+ c +"</p>");
$("#preview img").error(function () {
$("#preview img").attr("src" ,newName).css({'width': '400px', 'height': 'auto'});
});
$("#preview")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px")
.fadeIn("fast");
},
function(){
this.title = this.t;
$("#preview").remove();
});
$("a.preview").mousemove(function(e){
$("#preview")
.css("top",(e.pageY - xOffset) + "px")
.css("left",(e.pageX + yOffset) + "px");
});
};任何帮助都将成为学徒。
谢谢
Salman
发布于 2010-05-08 17:04:30
你的摘录与其他无关的代码有点臃肿/对我来说花太多时间看它很长时间,但我认为,理解你的问题,我认为这段代码片段,我写来处理超大型下拉导航,并确保它永远不会离开屏幕可能会帮助你。
function resize() {
var viewportWidth = window.innerWidth ? window.innerWidth : $(window).width();
if (jQuery.browser.msie) {
if(parseInt(jQuery.browser.version) == 7) {
viewportWidth -= 3;
}
}
$('ul#nav .sub').each(function() {
$(this).css({'display': 'block', 'left': ''});
var offset = $(this).offset().left + $(this).outerWidth(true) + 10;
if(offset > viewportWidth) {
ie6offset = $.browser.msie && $.browser.version.substr(0,1)<7 ? 30 : 0;
$(this).css({
'left': -(offset + 10 - viewportWidth) - ie6offset
});
}
});
}总之,该函数计算.sub的右侧边缘的真实偏移量。然后检查该偏移量与视口的宽度,同时考虑到浏览器的不一致。如果偏移量大于视口的宽度,则需要设置一个负值,以使元素保持在屏幕上。
https://stackoverflow.com/questions/2792851
复制相似问题