Lewis Day Lewis Day - 1 year ago 97
Javascript Question

bootstrap collapse after set height

What I'm trying to achieve is relatively simple after a set height I'm looking to collapse a div using bootstrap default attributes. Below I have written the javascript to trigger after a set height but what I'm unsure on is how to trigger the collapse with the id of

#more
.

function testScroll(ev){
if(window.pageYOffset>1100)alert('User has scrolled at least 400 px!');
}


<div id="more" class="collapse">test</div>


Thankyou in advance.

Answer Source

You can use the collapse function as defined in the bootstrap docs, by providing it the element id. You can also read more about it here.

function testScroll(ev){
    if(window.pageYOffset>1100) {
      alert('User has scrolled at least 400 px!');
      $('more').collapse('hide');
    } else {
      alert('User has not scrolled at least 400 px!');
      $('more').collapse('show');
    }
}


<div id="more" class="collapse">test</div>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download