0

我需要在 Windows 调整大小时动态更改图像边距。但是当我调整窗口大小时,我需要刷新页面,以便将图像放在正确的位置。有没有办法不刷新页面来获得右边距。

var iw = $(window).width();
        jQuery( document ).ready(function( $ ) {    


            updateContainer();
            $(window).resize(function() {
                updateContainer();
            });


        });     

        function updateContainer(){

            $( ".rimg" ).each(function() {                  
                var w =$(this).width();             
                if(w>iw){                       
                    marg = (w-iw);

                    $($(this)).css("margin-left", function() { return "-"+marg+"px" });
                }


            });
        }
4

1 回答 1

2

尝试使用以下脚本

    jQuery( document ).ready(function( $ ) {    
       $(window).resize(function() {
           var iw = $(window).width();
            updateContainer(iw);
        });


    });     

    function updateContainer(iw){

        $( ".rimg" ).each(function() {                  
            var w =$(this).width();             
            if(w>iw){                       
                marg = (w-iw);

                $($(this)).css("margin-left", function() { return "-"+marg+"px" });
            }


        });
    }

您已经声明了iwresize 函数的外部,因此它只会给出相同的窗口大小,请尝试在resize函数内部声明窗口大小

于 2013-10-11T06:55:46.547 回答