Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anchor doesn't work

I have a problem with link inside page. This is part of jQuery code I use in my page

$.fn.stopAtTop= function () {
  var $this = this,
      $window = $(window),
      thisPos = $this.offset().top,
      //thisPreservedTop = $this.css("top"),
      setPosition,
      under,
      over;

  under = function(){
    if ($window.scrollTop() < thisPos) {
        $this.css({
            position: 'absolute',
            top: ""
        });
        setPosition = over;
    }
  };

  over = function(){
    if (!($window.scrollTop() < thisPos)){
        $this.css({
            position: 'fixed',
            top: 0
        });
        setPosition = under;
    }
  };

  setPosition = over;

  $window.resize(function()
  {
    bumperPos = pos.offset().top;
    thisHeight = $this.outerHeight();
    setPosition();
  });

  $window.scroll(function(){setPosition();});
  setPosition();
};

And this is a example DEMO

When I scroll down everything works fine but when I want go to the top of the page it's impossible. I know that issue is that the script makes the div fixed, but I don't know how to fix it. Any ideas?

like image 485
MaSza Avatar asked Oct 30 '22 14:10

MaSza


1 Answers

Add a click handler that scrolls to the top of the page:

$("[href='#one']").click(function() {
    scrollTo(0, 0);
});

jsfiddle.net/jx8nmhfq/1

like image 108
gilly3 Avatar answered Nov 11 '22 02:11

gilly3