-1

I've created a function that has to run only if the window is wider than 769px. It works when the page loads, but not on resize...

It looks like this:

$(window).on('load resize', function () {
    if ($(window).width() >= 769) {
       ...funcion...
    }
});

EDITED:

Full code below

$(window).on('load resize', function () {
    if ($(window).width() >= 769) {
      var $element = $('#cont_quote');
      var $follow = $element.find('.img_quote');
      var followHeight = $element.find('.img_quote').outerHeight();
      var height = $element.outerHeight() - 300;
      var window_height = $(window).height();

      $(window).scroll(function () {
        var pos = $(window).scrollTop();
        var top = $element.offset().top;

        // Check if element is above or totally below viewport
        if (top + height - followHeight < pos || top > pos + window_height) {
          return;
        }

        var offset = parseInt($(window).scrollTop() - top);

        if (offset > 0) {
        $follow.css('transform', 'translateY('+ offset +'px)');
        }

      })
    }
});

HTML:

<section id="cont_quote">
    <article class="cont_q">
        Lorem ipsum 
        <img class="img_quote" src="img">
    </article>
</section>
Marco
  • 97
  • 2
  • 3
  • 11

2 Answers2

1

Try something like this:

$(document).ready(function(){
  $(window).resize(function(){
    if ($(window).width() >= 769) {
       ...funcion...
    }
  }
}

You can change on ready or on load, depending on what you need, but the the function should trigger on window resize.

  • `.resize()` is simply a shortcut for `on('resize'...` no reason this would change anything – charlietfl Apr 24 '18 at 12:03
  • And it's not, writing it in Jquery didn't change anything, compare this code with what he wrote and you'll see why his resize event wasn't triggered. – David Major Apr 24 '18 at 12:14
  • 1
    OP's code works fine here https://jsfiddle.net/vra13358/ So no idea what you mean by *"will see why it's not working"* since it does work – charlietfl Apr 24 '18 at 12:17
  • Then it's my bad, should have tested his code first rather than assume it didn't work. Tho is it a good practice to bind resize event with load on the same function especially if you are planning to have some more stuff happening on window load only? – David Major Apr 24 '18 at 12:26
  • As for good practice that is unclear from the limited snippet shown. Combining multiple events to use same handler is not unusual though. Note you also don't need `$(document).ready` since `window` always exists – charlietfl Apr 24 '18 at 12:28
0

I think you can try this solution this is just javaScript

    var onWindowResize = function(e) {
           width = e.target.outerWidth;
           //uncomment if need height = e.target.outerHeight;
           if(width >= 769) {

               //remove alert just added for debug
               alert("if");
        }
           else{
             //remove alert
               alert("else");
               }
           }
        window.addEventListener("resize", onWindowResize);
v8-E
  • 1,077
  • 2
  • 14
  • 21