admin管理员组文章数量:1396787
I am new in javascript and I want to perform some action when the div with is changed
in jquery, I use this code
var width = $(window).width();
$(window).resize(function(){
if($(this).width() != width){
width = $(this).width();
console.log(width);
}
});
but I want to do it using javascript help me please...
I am new in javascript and I want to perform some action when the div with is changed
in jquery, I use this code
var width = $(window).width();
$(window).resize(function(){
if($(this).width() != width){
width = $(this).width();
console.log(width);
}
});
but I want to do it using javascript help me please...
Share Improve this question asked Oct 4, 2016 at 6:31 Aditya DwivediAditya Dwivedi 2621 gold badge5 silver badges20 bronze badges 4- stackoverflow./questions/15205609/… – Ashok kumar Commented Oct 4, 2016 at 6:35
- Why would you not want to do this with jQuery? – Efekan Commented Oct 4, 2016 at 6:40
- 2 actually, I don't want to use jquery on my whole site because I have not included jquery on my site it take 91 kb and I am, try reduce the size of my website – Aditya Dwivedi Commented Oct 4, 2016 at 6:47
- Use ResizeObserver: stackoverflow./a/39312522/47528 – Dan Commented May 4, 2020 at 13:33
3 Answers
Reset to default 5You can use on-resize-event like this:
var body = document.getElementsByTagName("BODY")[0];
var width = body.offsetWidth;
if (window.addEventListener) { // all browsers except IE before version 9
window.addEventListener ("resize", onResizeEvent, true);
} else {
if (window.attachEvent) { // IE before version 9
window.attachEvent("onresize", onResizeEvent);
}
}
function onResizeEvent() {
bodyElement = document.getElementsByTagName("BODY")[0];
newWidth = bodyElement.offsetWidth;
if(newWidth != width){
width = newWidth;
console.log(width);
}
}
Subscribe to the onresize event
window.onresize = functionName
In Internet Explorer you can use "onresize", to trigger a JavaScript function whenever size of an element(div) or body is changed.
But "onresize" does not work the same way in other browsers. Mozilla Firefox will trigger the function only when body is resized not a div.
<body onresize="myFunction()">
will work in every browser
<div onresize="myFunction()">
Will work only in IE
本文标签: htmldetect when div width is change using javascriptStack Overflow
版权声明:本文标题:html - detect when div width is change using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744145906a2592835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论