jQuery 窗口调整大小
我有一段 JavaScript 代码,应该在调整浏览器窗口大小时处理。
resize.style.marginRight=(resize.offsetWidth-$(this)[0].offsetWidth)+'px'
尝试了这个东西(不起作用):
window.onresize = function(event) {
resize.style.marginRight=(resize.offsetWidth-$(this)[0].offsetWidth)+'px'
}
完整代码可用 - http:// jsbin.com/adelu3/2/(参见页面源代码)
Html(由脚本生成):
<div class="resizable-textarea">
<span>
<textarea class="resizable processed" cols="" rows=""></textarea>
<div class="resize"></div>
</span>
</div>
谢谢。
I have piece of javascript code, which should process when the browser window is resized.
resize.style.marginRight=(resize.offsetWidth-$(this)[0].offsetWidth)+'px'
Tryed this stuff (doesn't work):
window.onresize = function(event) {
resize.style.marginRight=(resize.offsetWidth-$(this)[0].offsetWidth)+'px'
}
Full code available - http://jsbin.com/adelu3/2/ (see page source)
Html (generated by the script):
<div class="resizable-textarea">
<span>
<textarea class="resizable processed" cols="" rows=""></textarea>
<div class="resize"></div>
</span>
</div>
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
offsetWidth
是元素的一个属性。在window.onresize
的回调代码中,this
是一个Window
,它没有offsetWidth。this
应该是什么? (链接代码中不存在onresize
事件。)如果要读取窗口宽度,请使用$(window).width()
。如果您想读取封闭范围中作为
this
的其他某个(祖先?)元素的宽度,则必须通过从resize
查找来找到该元素> 元素,或者保留对闭包中其他元素的引用,例如:(注意。
$(this)[0]
完全不执行任何操作。)offsetWidth
is a property of elements. In the callback code ofwindow.onresize
,this
is aWindow
, which doesn't have an offsetWidth.What is
this
supposed to be? (Theonresize
event is not present in the linked code.) If you want to read the window width, use$(window).width()
.If you want to read the width of some other (ancestor?) element that you had as
this
in the enclosing scope, you must either find that element by looking up from theresize
element, or retain a reference to the other element in a closure, eg.:(nb.
$(this)[0]
does precisely nothing.)自己做的。
这是一个混合解决方案,它共享@bobince 的代码和我自己的代码。
Made myself.
This is a mixed solution, it shares @bobince's code and my own.