最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to use "object.addEventListener("resize", myScript)" for a div element? - S

matteradmin7PV0评论

before i was trying to add this event listener to a div

i was using "scroll", this one works but "resize" is not working

and i can't find out why .

This is my code :

$('document').ready(function() {

  var content = document.getElementById("scroller");
  var tabs = document.getElementById("tabs");

  var than, now;
  than = tabs.clientHeight;

  content.addEventListener("resize", function(event) {
    now = tabs.clientHeight;

    alert(now + "&" + than);
  }, false);


});

JSFIDDLE DEMO

or is it possible something like this :

$('document').ready(function(){ 

                var content = document.getElementById("scroller");
                var hc , h;

                window.addEventListener('resize', function(event){

                    h = event.clientHeight;
               if(h>510) {
                $(".godown").fadeout("0");
                    }
               if(h<510) {
                content.addEventListener('scroll', function(event){
                 hc =  $('#scroller').scrollTop();






                    if (hc){
                   $(".godown").fadeOut("slow");
                   $(".gotop").fadeIn("slow");
                    }
 if (!hc){
                   $(".godown").fadeIn("slow");
                   $(".gotop").fadeOut("slow");

}
             }, false);
 }
                    }, false);
            });

before i was trying to add this event listener to a div

i was using "scroll", this one works but "resize" is not working

and i can't find out why .

This is my code :

$('document').ready(function() {

  var content = document.getElementById("scroller");
  var tabs = document.getElementById("tabs");

  var than, now;
  than = tabs.clientHeight;

  content.addEventListener("resize", function(event) {
    now = tabs.clientHeight;

    alert(now + "&" + than);
  }, false);


});

JSFIDDLE DEMO

or is it possible something like this :

$('document').ready(function(){ 

                var content = document.getElementById("scroller");
                var hc , h;

                window.addEventListener('resize', function(event){

                    h = event.clientHeight;
               if(h>510) {
                $(".godown").fadeout("0");
                    }
               if(h<510) {
                content.addEventListener('scroll', function(event){
                 hc =  $('#scroller').scrollTop();






                    if (hc){
                   $(".godown").fadeOut("slow");
                   $(".gotop").fadeIn("slow");
                    }
 if (!hc){
                   $(".godown").fadeIn("slow");
                   $(".gotop").fadeOut("slow");

}
             }, false);
 }
                    }, false);
            });
Share Improve this question edited Apr 29, 2016 at 14:31 Luarb Balla asked Apr 29, 2016 at 12:54 Luarb BallaLuarb Balla 1691 gold badge3 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

You need to add the event listener to the window object, as it is the window resizing, not the element. window.addEventListener("resize", function(event) {...});

I have updated your jsfiddle here where you can see it working.

$('document').ready(function() {

  var content = document.getElementById("scroller");
  var tabs = document.getElementById("tabs");

  var than, now;
  than = tabs.clientHeight;

  window.addEventListener("resize", function(event) {
     now = tabs.clientHeight;
     alert(now + "&" + than);
     }, false);
});

If someone still interested

You can use ResizeObserver

const resizeObserver = new ResizeObserver((entries) => {
  for (const entry of entries) {
    console.log('Width', entry.contentRect.width)
  }

  console.log("Size changed");
});

const divEl = document.getElementById('el');

resizeObserver.observe(divEl);
<div id="el">Some Element</div>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far