0

I can't understand why this code don't works correctly:

$(document).ready(function() {
  $(window).scroll(function() {
    console.log($(window).scrollTop())
    if ($(window).scrollTop() < 626) {
      $('#shop-cart').addClass('shopcart-hidden').removeClass('shopcart-fixed');
    }
    if ($(window).scrollTop() > 625) {
      $('#shop-cart').removeClass('shopcart-hidden').addClass('shopcart-fixed');
    }
  });
});

The idea is to hide shopping cart information when page loads and to show it only when user scrolls down a little bit. If user scrolls up the information should be hidden again. I've got div with id - shop-cart and some php code inside it, also I've got CSS class defined in style.css:

#shop-cart {
  background:#757575;
}
.shopcart-fixed {
  float:right;
  right:0;
  z-index:100;
  position:fixed;
}
.shopcart-hidden {
  display:none;
}
5
  • What doesn't work? Can you post the HTML as well? Commented Feb 10, 2017 at 21:24
  • Why are you using two if? Use else for the second one! Commented Feb 10, 2017 at 21:25
  • 1
    use toggleClass to switch from one class to another instead of add / remove. Then you only need one test. Commented Feb 10, 2017 at 21:27
  • The code works for me. Here's a fiddle: jsfiddle.net/f3pr4tjL/1 Commented Feb 10, 2017 at 21:29
  • bejado, there is nothing speciall in HTML, just some php code which calls shopping cart. The strange thing is that display:none property don't removes :( BTW this code works fine in online sandbox too, but don't works on localhost. I've checked this right now :( Commented Feb 11, 2017 at 7:35

2 Answers 2

2

Hide the bar by default using CSS, then you only need a single class that you can toggle that will show it as fixed once you pass the scrollTop() you've specified.

$(document).ready(function() {
  $shopCart = $('#shop-cart');
  $(window).scroll(function() {
    var $scrollTop = $(window).scrollTop();
    if ($scrollTop < 626) {
      $shopCart.removeClass('shopcart-fixed');
    }
    if ($scrollTop > 625) {
      $shopCart.addClass('shopcart-fixed');
    }
  });
});
body {
  min-height: 500vh;
}
.shop-cart {
  background:#757575;
  display: none;
}
.shopcart-fixed {
  right:0;
  z-index:100;
  position:fixed;
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="shop-cart" class="shop-cart">shop-cart</div>

Sign up to request clarification or add additional context in comments.

4 Comments

I've tryed this code in sandboxes online - everything worked fine, but when I paste it to my localhost, then it stops work. It makes everything, except removing display:none :-/ If I remove display:none from CSS - everything works fine. Maybe it is so because of jquery.min.js conflict with bootstrap.min.js?
It started to work normally, after I added display:block!important; in my CSS file. Thanks for answer
One more question, how to check in JS is it frontpage or no? I'd like to show this menu only on frontpage. Or maybe easier will be to check in php using is_frontpage ?
Thanx alot, I've decided to exclude JS in final version and simply call my cart form in header. But anyway, yours advices are very usefull and I will use them in future.
0

jQuery.toggleClass has a second parameter which tell wether to remove the class or to add it. You can use it like this:

$(document).ready(function() {
    $(window).scroll(function() {
        var scrollTest = ($(window).scrollTop() < 625); // will be true or false
        $('#shop-cart').toggleClass('shopcart-hidden', scrollTest); // will add if scrollTest is true, remove otherwise
        $('#shop-cart').toggleClass('shopcart-fixed', !scrollTest); // will add if scrollTest is false, remove otherwise
    });
});

Note: why are you using two classes to do something you can do with only one class? Just put the CSS of one class as the default and then override that CSS when the other class is set.

1 Comment

Thanks for advice, I'm quite new in JS, that's why making mistakes. The strange thing is that display:hidden don't removes :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.