html - detect when div width is change using javascript - Stack Overflow

I am new in javascript and I want to perform some action when the div with is changedin jquery, I use t

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
Add a ment  | 

3 Answers 3

Reset to default 5

You 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

相关推荐

  • html - detect when div width is change using javascript - Stack Overflow

    I am new in javascript and I want to perform some action when the div with is changedin jquery, I use t

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信