I am trying to simply add the class active on window scroll and remove it when not scrolled.
I tried to follow these directions located here:
jQuery : add css class to menu item based on browser scroller position
However, I still must be doing something wrong. Nothing is happening to the id myCart when scrolling.
I have the following html:
<div id="myCart" class="active">
<div class="add-to-cart">
<div class="clearfix qty_ticker">
<label for="qty">Qty:</label>
<span class="marker_qty_left"> </span>
<input id="qty" class="input-text qty" type="text" title="Qty" value="1" maxlength="12" name="qty">
<span class="marker_qty_right"> </span>
</div>
<button class="button btn-cart" onclick="productAddToCartForm.submit(this)" title="Add to Cart" type="button">
<span>
<span>Add to Cart</span>
</span>
</button>
</div>
</div>
And the following javascript or jquery:
<script type="text/javascript">
$(window).scroll(function() {
// find the id with class 'active' and remove it
$("#myCart").removeClass("active");
// get the amount the window has scrolled
var scroll = $(window).scrollTop();
// add the 'active' class to the correct id based on the scroll amount
if (scroll <= 500) {
$("#myCart").addClass("active");
}
});
</script>
Any help would be great!
You are missing “)” at the end of your script which is causing the javascript not to compile.
Code should be
When you are using both jquery and prototype, the jquery’s $ global variable conflicts with prototypes $ global variable. One way to fix that is to use jquery’s jQuery variable as such