javascript图像调整窗口大小

标签 javascript image-resizing mousemove css

我为我的网站开发的 JavaScript 有问题。我有将鼠标悬停在上面时打开的图片。

首先,我的脚本会计算图像应该显示在窗口的右侧还是左侧:

$("html,body").live("mousemove", function (e) {
    //console.log("mousemove: "+e.pageX)  
    var width_window = $(window).width();
    var center = width_window / 2;
    if (e.pageX < center) {
        side = 'left';
    } else {
        side = 'right';
    }
});

然后,一旦我们知道图像将显示在窗口的哪一侧,我就有另一个脚本来调整图像的大小,这取决于我的窗口的高度和宽度,包括边距:

this.resizeImg = function (img, offset) {
    var d = new Date();
    //console.log(d, side);

    var window_height = $(window).height();
    var img_height = $(img).height();
    var img_top = $(img).offset().top;

    var window_width = $(window).width();
    var img_width = $(img).width();
    var img_left;
    side == 'left' ? img_left = offset.left : img_left = window_width - offset.left;

    console.log(window_width, img_left)
    var image_resize_height = window_height - img_top - 20;
    var image_resize_width = window_width - img_left - 20;

    if (img_height + img_top > window_height && img_width + img_left > window_width) {
        console.log("h w")

        if (image_resize_width > image_resize_height) {
            $(img).css('height', image_resize_height + 'px').css("width", "auto");
        } else {
            $(img).css('width', image_resize_width + 'px').css("height", "auto");
        }

    } else if (img_height + img_top > window_height) {
        //console.log("h")
        $(img).css('height', image_resize_height + 'px').css("width", "auto");
    } else if (img_width + img_left > window_width) {
        //console.log("w")
        $(img).css('width', image_resize_width + 'px').css("height", "auto");
    } else {
        //console.log("non")
    }
};

它几乎可以工作,但有时我的图像会超出窗口宽度或高度。我找不到解决方案...

这是我的 CSS:

.vignette {
        max-height: 800px;
        max-width : 800px;
        z-index : 2;
        top : 25px;
}

.info{
    position : relative;
}

.info img {
    position : absolute;
    display : none;
    cursor : pointer;
}

我在 jsFiddle 中的完整脚本:http://jsfiddle.net/CrnNZ/

这是我网站的链接:http://olivierlellouche.com/

非常感谢您的帮助!

最佳答案

您是否注意到将图像向下移动 25 像素 的事实:

.vignette {
    top : 25px;
}

我看到的唯一高度调整是 20px:

var image_resize_height = window_height - img_top - 20;

您可能只需要在计算中减去几个像素?

或者更好的是:

var img_top = $(img).offset().top;

可能是偏移区域的顶部,而不是图像的原始顶部。在这种情况下,您仍然需要为此减去 25px。

(来自您的网站)另一件可能有用的事情是始终启用或始终禁用右侧的垂直滚动条。或者当文本区域不是滚动条时,将文本区域的大小调整为小于可用区域。 (不幸的是,我根本无法让你的 jsfiddle 工作,我能看到的唯一错误是垂直计算错误。我看不到任何水平错误。)

如果将高度减去几个像素,问题是否仍然存在?

我无法从您的代码中看出,但是它会放置图像然后重新调整大小吗?在尝试放置图像之前计算可用尺寸可能是更好的主意,这样一来它就不会改变尺寸。

编辑:

在查看了您的窗口小得多的网页后,我想到了其他事情。 $(window).height()$(document).height() 不同。请参阅:$(window).height() vs $(document).height如果它们不相同,您可能需要以不同方式计算剩余页面。

关于javascript图像调整窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19380233/

相关文章:

javascript - 使用 ajax 提交表单会导致 Charset 出现奇怪的行为

javascript - 无法在 Angular 2 应用程序中提交 HTML 表单

javascript - 当 Canvas 不是全屏 r74 时单击 three.js 中的对象

python - Django:更改图像大小并上传到 S3

c# - 模拟鼠标光标在 c# 中在两个坐标之间移动

d3.js - 变换后的D3鼠标坐标

javascript - 回调、导出和 JavaScript

android - 调整无宽高比的图像大小

html - 强制同一行中的多个图像按比例调整大小,而不是将它们移动到新行

c++ - 使用 GLUT_DOUBLE 模式绘制形状