0

I'm trying to trigger an element to slide up when I reach an anchor (EDIT: via scroll). The anchor ID is #blacksection and the element class is .vc_tta-panels-container. I read on another thread that the best way to use an anchor to trigger a JS animation is to trigger an onclick event from the anchor using something like:

jQuery("#blacksection").trigger('click');

And then I'm using;

  jQuery("#blacksection").on('click', function() {
    jQuery(".vc_tta-panels-container").slideUp();

as the remaining JS. So I have this:

<script>
jQuery("#blacksection").trigger('click');
  jQuery("#blacksection").on('click', function() {
    jQuery(".vc_tta-panels-container").slideUp();
});
</script>

Which isn't working, only just started learning JS and it's baffling my mind, can anyone shed some light on triggering slideUp() for a class when the user scrolls past an anchor (#blacksection)?

1 Answers1

1

The click event needs to be defined before you call it with the .trigger() function. Also, depending on what sort of default behaviour #blacksection has on the click event, you may wish to use .triggerHandler() instead, which invokes only the user-defined events:

jQuery("#blacksection").on('click', function() {
    jQuery(".vc_tta-panels-container").slideUp();
});
jQuery("#blacksection").triggerHandler('click');

EDIT:

Given your comment, which clarifies things somewhat, the above solution won't work. You want the .vc_tta-panels-container element to slide up when the #blacksection element comes into view on screen during/after scrolling.

Thankfully, someone already has this solution here: Check if element is visible after scrolling

To tailor it to your problem, here is a snippet with some HTML and appropriate JS:

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

  }

  Utils.prototype = {
      constructor: Utils,
      isElementInView: function (element, fullyInView) {
          var pageTop = $(window).scrollTop();
          var pageBottom = pageTop + $(window).height();
          var elementTop = $(element).offset().top;
          var elementBottom = elementTop + $(element).height();
          
          if (fullyInView === true) {
              return ((pageTop < elementTop) && (pageBottom > elementBottom));
          } else {
              return ((elementTop <= pageBottom) && (elementBottom >= pageTop));
          }
      }
  }
  

  var Utils = new Utils();
  
   $('#scrollsection').scroll(function() {
    var isElementInView = Utils.isElementInView($('#blacksection'), false);

    if (isElementInView) {
        $('.vc_tta-panels-container').slideUp();
    } else {
        $('.vc_tta-panels-container').slideDown();
    }
  });
  

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div class="vc_tta-panels-container" style="position: fixed; width: 100%; height: 100px; background-color: #f00">
  <span>I will slide up when the word "BAZ" comes into view below</p>
</div>
<div id="scrollsection" style="position:fixed; top: 100px; width: 100%; bottom: 0; overflow-y: scroll">
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    <div id="blacksection">BAZ</div>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
    .<br>
</div>
Community
  • 1
  • 1
e_i_pi
  • 4,590
  • 4
  • 27
  • 45