F
F
fajjet2015-11-02 00:10:50
css
fajjet, 2015-11-02 00:10:50

What is the correct way to use addEventListener?

I'm designing a website that will use a decent amount of animation. So, you need to support older versions of browsers. Animation will happen mostly by event. As far as I know, older browsers don't support multi-step event-based functions on a single element. Therefore, when resizing the browser window, for example, you need to call all functions in one place. Let's say we have a slider script. Inside it, I usually wrote a nested function that updated certain parameters for the slider depending on the current browser settings and was attached to the risize event. That is, when resizing, for example, the parent block changed its height. What is the idea? - When this function was called inside this function, only a small piece of code was called that updated the values ​​of the variables using local variables, without creating anything and without doing the reinitialization of variables. Let's say

function slider(){
var per1 = alalslalsal;
var per2 = kakakakaka;
var per3 = jujujujujuju;

function getProp(){
     per3 = kkk.offsetHeight; // При ресайзе используем переменные выше и выполняем только маленький клочок кода
}
window.addEventListener("resize",getProp);

function action(){
   Работа слайдера
}
button.addEventListener("click",action);
}
window.addEventListener("load",s;ider);

What do you advise?)) I hope I explained it clearly) I need to put all the functions that should change during resizing into one -
window.onresize = function(){
      Сюда!!!!!!!!!!!!!
       function1();
       function2();
      function3();
      и т.д.
}

You understand that with such a call, the script will recreate the variables, reselect the selector, recalculate the values, etc. If, for example, to make a flag that the function has been launched and there is no need to re-create them, then for some reason they are not visible.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sasha, 2015-11-02
@madmages

firstly
, if there is a need to support old browsers, then I recommend paying attention to the processing of events not through addEventListener, but by throwing an html attribute on the element. thus, the probability of multiple triggering disappears (suddenly there is an error somewhere in the code) and old browsers will pull the current on the way.
secondly,
I did not find an explicit question in your text

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question