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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744145906a4560421.html
评论列表(0条)