slider effect does not work with jQuery

Asked

Viewed 40 times

-2

I have the following structure:

    <body> 

        <div id="topo">

            <section class="topo">

                <div>

                    <ul id="menuTopo">

                        <li><a href='#alvo'>Link</a></li>

    ...         

I even put the div on a var

    var topo = $('body div#topo section.topo div');

So then I did the following:

    $(topo).find('ul#menuTopo li a[href^="#"]').click(function () {
      alert();
      e.preventDefault();
      var id = $(topo).find('ul#menuTopo li a').attr('href'),
        targetOffset = $(id).offset().top;

      $('html, body').animate({
        scrollTop: targetOffset - 100
      }, 500);
    });

and the return I had was that the effect does not occur! Nor the alert works

But the ul is there.

If I do:

    $(topo).find('ul#menuTopo li a').click(function () {
    alert();

The alert works!

Where am I going wrong?

I need to do a sliding effect on links# menu

  • How would that "sliding effect"?

  • And why not just use the dial a[href^="#"]?

  • Sumback, is an effect that instead of clicking the loink goes down as a flash to the target #, it goes down smooth...

  • iamdim: by the fact that I only want this effect ul only of links and not on the whole site

1 answer

1


I changed a few points in your code, I hope it helps.

    var topo = $('body div#topo section.topo div');

   //Adição do parametro 'e'

    $(topo).find('ul#menuTopo li a[href^="#"]').click(function (e) {
        //alert();
        e.preventDefault();
        //Refenciar 'topo' como classe css '.topo'
        var id = $(".topo").find('ul#menuTopo li a');
        targetOffset = $(id).offset().top;

        $('html,body').animate({
            scrollTop: (targetOffset - 100)
        }, 500);
    });
  • It didn’t work! But, that $(this) wouldn’t just get the $(top)?

  • what’s picking up is that if I just put the element a, Alert works. but if u paste all the[href ="#"]', then Alert doesn’t work

Browser other questions tagged

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