简体   繁体   English

阻止js图片从屏幕上弹出

[英]stop js image pop up from going off screen

Using the code below, how can I stop it from going off the screen? 使用下面的代码,如何阻止它离开屏幕? If you have an image on the far right of the browser window, the code expands the image to the right, so now the expanded image will be off the screen. 如果浏览器窗口的最右边有一个图像,则代码会将图像扩展到右侧,因此现在扩展的图像将不在屏幕上。 I would like it to load to the left if it wants to load off the screen (to the right). 如果要从屏幕上加载(右侧),我希望将其加载到左侧。 I hope that makes sense. 我希望这是有道理的。 I would like to 'contain' the popup inside the browser window -- more specifically it needs to be contained to #page_container (which wraps the entire page) 我想在浏览器窗口中“包含”弹出窗口-更具体地说,它需要包含在#page_container中(包装整个页面)

I'm not sure where the original code came from, but im trying to modify it. 我不确定原始代码来自哪里,但是我正在尝试修改它。

any help is appreciated. 任何帮助表示赞赏。

this.screenshotPreview = function(){    

    xOffset = 10;
    yOffset = -10;

$("a.screenshot").hover(function(e){
    this.t = this.title;
    this.title = "";    
    var c = (this.t != "") ? "<br/>" + this.t : "";
    $("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='preview'/>"+ c +"</p>");                                 
    $("#screenshot")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px")
        .fadeIn("fast");                        
},
function(){
    this.title = this.t;    
    $("#screenshot").remove();
}); 
$("a.screenshot").mousemove(function(e){
    $("#screenshot")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px");
});         
};

here is the fiddle 这是小提琴

http://jsfiddle.net/63HA4/ http://jsfiddle.net/63HA4/

First of all you are mixing the "x" property with the "yOffset" that probably not what you intend. 首先,您将“ x”属性与“ yOffset”混合使用,这可能不是您想要的。 Triple check that. 三重检查。

Change your mousemove method for something like this, please note the check I'm adding to see if the future position is becoming negative (out of the parent container) 将您的mousemove方法更改为类似这样的方法,请注意我要添加的检查以查看将来的头寸是否变为负数(在父容器之外)

$("a.screenshot").mousemove(function(e){
    var x = (e.pageY - xOffset < 0)? 0: e.pageY - xOffset,
        y = (e.pageY + yOffset < 0)? 0: e.pageX + yOffset;
    $("#screenshot")
    .css("top",y + "px")
    .css("left",x + "px");

Then again, check the math, this is not a final code but it will give you an idea of what you need to do 然后再次检查数学,这不是最终代码,但可以让您大致了解需要做什么

Here is the solution that I implemented, it works for me. 这是我实施的解决方案,对我有用。

this.screenshotPreview = function(){    

xOffset = -10;
yOffset = -10;

$("a.screenshot").hover(function(e){
    this.t = this.title;
    this.title = "";    
    var c = (this.t != "") ? "<br/>" + this.t : "";
    $("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='preview'/>"+ c +"</p>");                                 
    $("#screenshot")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(e.pageX + yOffset) + "px")
        .fadeIn("fast");                        
},
function(){
    this.title = this.t;    
    $("#screenshot").remove();
}); 
$("a.screenshot").mousemove(function(e){
    var _width = jQuery('#screenshot').outerWidth(),
    _outerWidth = document.body.offsetWidth;

    var x = e.pageX + yOffset;
    if(x >(_outerWidth - _width)){
        x = _outerWidth - _width;
    }

    $("#screenshot")
        .css("top",(e.pageY - xOffset) + "px")
        .css("left",(x) + "px");
});         
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM