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>