Fixed jquery scroll at top by clicking on a div #

Asked

Viewed 68 times

0

Next people I have a problem because when I clicked on a menu anchored by a #id it did not stay with the scroll at the top and so the content was cut because now I put this function and it worked perfectly however when I go to look at the error console

Jquery:

 $('.sub-menu a').click(function() {

    var anchor;
    var link = $(this).attr('href');
    if (link.indexOf('#') !== -1) {
        anchor = '#' + link.split('#').pop();
    }

    $('.id-sub').slideUp(2000);
     if (anchor !== undefined) {
        $(anchor).slideDown(2000).addClass('fixed');
        $('html,body').animate({ 'scrollTop': $(anchor) }, function(){
            window.location.hash = target;
        });
     }

});

this is the mistake :

Uncaught ReferenceError: target is not defined
at HTMLBodyElement.<anonymous> (scripts.js?ver=4.9.7:30)
at HTMLBodyElement.d.complete (jquery.js?ver=1.12.4:4)
at i (jquery.js?ver=1.12.4:2)
at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
at i (jquery.js?ver=1.12.4:4)
at n.fx.tick (jquery.js?ver=1.12.4:4)

but I’ve tried to take this line :

     function(){
        window.location.hash = target;
    });

I have tried to create a var like null and nothing works error until I come out with these above fixes however scroll does not get fixed at the top could help me?

  • Which version of jQuery you are using?

  • version is is 1.12.4

  • @Filipenickelsala from what I understand target is the name of the anchor you want to go to source:https://www.w3schools.com/jsref/prop_loc_hash.asp

  • I don’t know if it can be that, but sometimes it’s worth testing a newer version, because jQuery is already in version 3.3.1, if I’m not mistaken. This version of yours is very old... Tb I do not know if it is correct to not return the value of the variable, even if null or undefined var anchor;

  • am using wordpress will there be any problem? I have already put in place of target the Anchor however remains same error

  • Test with a newer version just to see if you solve... https://code.jquery.com/ click on Uncompressed and take the CDN 3.x link and test

  • beauty I’ll test you already I speak worth

  • <!--$('.sub-menu a'). click(Function() { var Anchor; var link = $(this).attr('href'); var target = $(this).attr("href"); //Get the target if (link.indexof('#') !== -1) { Anchor = '#' + link.split('#'pop).() } $('.id-sub').slideup(2000); if (Anchor !== Undefined) { $(Anchor).slideDown(2000).addClass('Fixed'); $('html,body').Animate({ 'scrollTop': $(Anchor) }, 600, Function(){ window.location. = ; }); } });-->

  • I did the same as above but it didn’t work and already updated jquery for new version

  • updated Jquery now or without target works more kkk I just want the scroll to be fixed at the top when it is called to (Anchor)

Show 5 more comments

1 answer

0

//Efeito SliderDown/SliderUp
$('.sub-menu a').click(function() {

    var anchor;
    var link = $(this).attr('href');
    if (link.indexOf('#') !== -1) {
        anchor = '#' + link.split('#').pop();
        console.log('anchor',anchor);
    }

    $('.id-sub').slideUp(2000);
    if (anchor !== undefined) {
        $(anchor).slideDown(2000).addClass('fixed');       
        $('html,body').animate({ 'scrollTop': $(anchor) }, 600, function(){
            window.location.hash = target;
        })
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.