简体   繁体   English

jquery扩展和调整大小的问题

[英]jquery expanding and resizing problem

Im using this jquery to try and control how wide and high a div opens depending on the screen resolution this is the code im using but it doesn't seem to be having any effect apart from cropping my image. 我正在使用这个jquery来尝试控制div打开的宽度和高度,这取决于屏幕分辨率,这是我正在使用的代码,但除了裁剪我的图像之外它似乎没有任何影响。 I say it doesnt seem to work becuase it leaves a big space and when I look at firebug it tells me the box has expanded to the 600px x 488px when im viewing in the lower resolution. 我说它似乎没有工作,因为它留下了一个很大的空间,当我看到萤火虫它告诉我,当我在较低的分辨率观看时,盒子已经扩展到600px x 488px。

Im not sure if the images are pushing the div out the that size because the pictures are exactly 600px x 488px but I need them to be the same file just smaller for dynamic PHP gallery updating in the future, how can I fix this code and how can I easily resize the images depending on the resolution? 我不确定图像是否正在将div推出那个大小,因为图片正好是600px x 488px但是我需要它们是相同的文件,对于将来动态PHP库更新来说更小,我该如何修复此代码以及如何我可以根据分辨率轻松调整图像大小吗?

$(document).ready(function(){
if ((screen.width>=1440) && (screen.height>=764)) {


$("#slideshow_box")
    .animate({"height": "600px"}, 500)
    .animate({"width": "488px"}, 500);

}
else  {

$("#slideshow_box")
    .animate({"height": "400px"}, 500)
    .animate({"width": "288px"}, 500);


}
});

DEMO DEMO

As you can se HERE even if you resize your screen the calculated width is actually = your brand new ;) screen - size! 你可以在这里查看即使你调整屏幕大小,计算出的宽度实际上是=你全新的;)屏幕尺寸! To get the actual 'screen' (window!) size in your browser you can use 要在浏览器中获得实际的“屏幕”(窗口!)大小,您可以使用

$(window).width(); and $(window).height(); $(window).height();

$(document).ready(function(){
    var winW = $(window).width();
    var winH = $(window).height();
    alert("Window width is: "+winW+"px, window height is: "+winH+'px');
    if ((winW>=1440) && (winH>=764)) {
        $("#slideshow_box")
            .animate({"height": "600px"}, 500)
            .animate({"width": "488px"}, 500);
    } else {
        $("#slideshow_box")
            .animate({"height": "400px"}, 500)
            .animate({"width": "288px"}, 500);   
    }
});

HERE you can see it in action, just resize the frame. 在这里你可以看到它在行动,只需调整框架的大小。

$(window).resize(function() {
    $('#size').html(
        ' Window width: '+$(window).width()+
        '<br> Window height: '+$(window).height()
    );
});

Hum, can't figure out your problem...seems to work for me, see http://jsfiddle.net/EtEzN/2/ 嗯,无法弄清楚你的问题...似乎对我有用 ,请参阅http://jsfiddle.net/EtEzN/2/

For sure, your snippet resizes DIV layer only, so you have to resize containing images too! 当然,您的代码段仅调整DIV图层的大小,因此您还必须调整包含图像的大小!

EDIT: Fiddle is updated, so script regards browsers document height instead of screen height (remember: screen resolution <> browser resolution <> document resolution) 编辑:小提琴更新,所以脚本认为浏览器文档高度而不是屏幕高度(请记住:屏幕分辨率<>浏览器分辨率<>文档分辨率)

The images are indeed pushing out your div . 这些图像确实在推动你的div If you want to change image size, then you can't just adjust the size of the containing div , you have to change the size of the img itself. 如果你想改变图像大小,那么你不能只调整包含div的大小,你必须改变img本身的大小。

I'm assuming that the image is intended to be the same size as the div that contains it. 我假设图像的大小与包含它的div大小相同。 Thus, your code should only require a slight modification: 因此,您的代码应该只需要稍作修改:

$(document).ready(function(){
if ((screen.width>=1440) && (screen.height>=764)) {

// Since the image is already the same size as the div,
// don't change the image
$("#slideshow_box")
    .animate({"height": "600px"}, 500)
    .animate({"width": "488px"}, 500);
}
else  {

// Resize the image along with the div
$("#slideshow_box, #slideshow_box img")
    .animate({"height": "400px"}, 500)
    .animate({"width": "288px"}, 500);
}
});

Also, the screen properties signify the resolution of the client's display screen, not the actual size of the browser window. 此外, screen属性表示客户端显示屏幕的分辨率,而不是浏览器窗口的实际大小。 Since you're using jQuery, you can use $(window).width() and $(window).height() , or if you're ever using plain JS, the window.innerWidth and window.innerHeight properties (for Firefox), or document.body.clientWidth for IE, although browser compatibility is annoying for this, so I'd probably stick with jQuery or another library. 既然你正在使用jQuery,你可以使用$(window).width()$(window).height() ,或者如果你曾经使用过普通的JS,那么window.innerWidthwindow.innerHeight属性(适用于Firefox) ),或IE的document.body.clientWidth ,虽然浏览器兼容性很烦人,所以我可能会坚持使用jQuery或其他库。

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

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