Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Links in page don't work

I have problem with link in one page. It work when i want scroll down, but when i want scroll up it stay in the same place. This is part of code:

(function($){
/* Store the original positions */
var d1 = $('.one');
var d1orgtop = d1.position().top;
var d2 = $('.two');
var d2orgtop = d2.position().top;
var d3 = $('.three');
var d3orgtop = d3.position().top;
var d4 = $('.four');
var d4orgtop = d4.position().top;

/* respond to the scroll event */
$(window).scroll(function(){
    /* get the current scroll position */
    var st = $(window).scrollTop();

    /* change classes based on section positions */
    if (st >= d1orgtop) {
        d1.addClass('latched');
    } else {
        d1.removeClass('latched');
    }
    if (st >= d2orgtop) {
        d2.addClass('latched');
    } else {
        d2.removeClass('latched');
    }
    if (st >= d3orgtop) {
        d3.addClass('latched');
    } else {
        d3.removeClass('latched');
    }
    if (st >= d4orgtop) {
        d4.addClass('latched');
    } else {
        d4.removeClass('latched');
    }
});

And example in JSFIDDLE JSFIDDLE

When i click href on top of the page it scroll down. But when i click href on the bottom, nothing happens. Where is my fault?

like image 686
MaSza Avatar asked Mar 14 '23 02:03

MaSza


1 Answers

DEMO

Because of your style position:fixed on .latched class. You remove it you fix it but yea it effects the original functionality as per my visualization. so as an alternative, I have below jquery hack which actually functions as required.

$('a[href="#intro"]').on('click',function(){
  $(d1,d2,d3,d4).removeClass('latched');  
  //on click of #intro element you just remove latched class from all the elements
})
like image 185
Guruprasad J Rao Avatar answered Mar 23 '23 12:03

Guruprasad J Rao