Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery窗口大小调整_Javascript_Resize - Fatal编程技术网

Javascript jQuery窗口大小调整

Javascript jQuery窗口大小调整,javascript,resize,Javascript,Resize,我有一段javascript代码,在调整浏览器窗口大小时,应该处理该代码 resize.style.marginRight=(resize.offsetWidth-$(this)[0].offsetWidth)+'px' 尝试过这种东西(不起作用): 完整代码可用-(参见页面来源) Html(由脚本生成): 谢谢 $(this)[0].offsetWidth offsetWidth是元素的属性。在window.onresize的回调代码中,此是一个窗口,它没有offsetWidth 这个

我有一段javascript代码,在调整浏览器窗口大小时,应该处理该代码

resize.style.marginRight=(resize.offsetWidth-$(this)[0].offsetWidth)+'px'
尝试过这种东西(不起作用):

完整代码可用-(参见页面来源)

Html(由脚本生成):


谢谢

$(this)[0].offsetWidth
offsetWidth
是元素的属性。在
window.onresize
的回调代码中,
是一个
窗口
,它没有offsetWidth

这个应该是什么?(链接代码中不存在
onresize
事件。)如果要读取窗口宽度,请使用
$(window).width()

如果要读取封闭范围中作为
this
的其他(祖先?)元素的宽度,必须从
resize
元素中查找该元素,或者在闭包中保留对其他元素的引用,例如:

var that= this;
$(window).resize(function() {
    resize.style.marginRight= resize.offsetWidth-that.offsetWidth+'px'
});
(注意,
$(这个)[0]
完全不起作用。)

让我自己

这是一个混合解决方案,它共享@bobince的代码和我自己的代码

var tarea= this;
function resize_width(){ resize.style.width= tarea.offsetWidth+'px'}
resize_width();
$(window).resize(function() {resize_width()});

@vsync-它在加载时提供右边距,但在调整浏览器窗口大小时不会更改。抱歉,我没有足够的javascript知识。你能给出完整的代码吗?这不起作用-$(window).width(函数(){resize.style.marginRight=(resize.offsetWidth-$(This)[0].offsetWidth)+'px')});这也是-$(window).width(resize.style.marginRight=(resize.offsetWidth-$(this)[0].offsetWidth)+'px');谢谢它并非每次都有效,有时会给出负边距。因此,下面是一个html:
var that= this;
$(window).resize(function() {
    resize.style.marginRight= resize.offsetWidth-that.offsetWidth+'px'
});
var tarea= this;
function resize_width(){ resize.style.width= tarea.offsetWidth+'px'}
resize_width();
$(window).resize(function() {resize_width()});