Effect of unwanted scroll

Asked

Viewed 265 times

1

I’m using the tab of the materialize, the switch works normally, but every time I click on a tab moves the page to the top, I don’t know why, I believe it has to do with the smooth scroll effect, but I’m not sure

<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css" media="screen,projection">


<div style='margin-top: 500px' id='tudo' class="col s12">
  <ul class="tabs">
    <li class="tab col s4"><a class='active' href="#minhas">Minhas</a></li>
    <li class="tab col s4"><a href="#favoritas">Favoritas</a></li>
    <li class="tab col s4"><a href="#publicas">Públicas</a></li>
  </ul>

  <!-- LISTA DE MINHAS -->
  <div class="row" id="minhas"></div>

  <!-- LISTA FAVORITAS -->
  <div class="row" id="favoritas"></div>

  <!-- LISTA PÚBLICAS -->
  <div class="row" id="publicas"></div>
</div>

<footer>
  <!-- Import JQuery.js -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <!-- Smooth scrolling effect -->
  <script>
    $(document).scroll(function() {
      $('a[href^="#"]').on('click', function(e) {
        e.preventDefault();

        var target = this.hash;
        var $target = $(target);

        $('html, body').stop().animate({
          'scrollTop': $target.offset().top
        }, 900, 'swing');
      });
    });
    $(document).ready(function() {
      $('.tabs').tabs();
    });
  </script>

  <!-- Import materialize.js-->
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
</footer>

  • Don’t you want the page to go to the top when switching tab? This is the unwanted effect?

  • @guastallaigor because this tab is in the middle of html, where different things are shown depending on the tab, it is very bad to click on favorites to see your favorites and be played to the top and have to scroll down everything again to see the content

  • Aham quiet, just wanted to make sure you understood correctly, I’ll try something here without taking your smooth scroll effect.

3 answers

1

I don’t understand the reason for the event click be inside the event scroll, so I removed (I left commented with //).

But you can solve the problem by taking the offset().top of the clicked element containing the hash, thus:

'scrollTop': $(this).offset().top

See the code:

<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css" media="screen,projection">


<div style='margin-top: 500px' id='tudo' class="col s12">
  <ul class="tabs">
    <li class="tab col s4"><a class='active' href="#minhas">Minhas</a></li>
    <li class="tab col s4"><a href="#favoritas">Favoritas</a></li>
    <li class="tab col s4"><a href="#publicas">Públicas</a></li>
  </ul>

  <!-- LISTA DE MINHAS -->
  <div class="row" id="minhas">minhas</div>

  <!-- LISTA FAVORITAS -->
  <div class="row" id="favoritas">favs</div>

  <!-- LISTA PÚBLICAS -->
  <div class="row" id="publicas">pubs</div>
</div>

<footer>
  <!-- Import JQuery.js -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <!-- Smooth scrolling effect -->
  <script>
//    $(document).scroll(function() {
      $('a[href^="#"]').on('click', function(e) {
        e.preventDefault();

        $('html, body').stop().animate({
          'scrollTop': $(this).offset().top
        }, 900, 'swing');
//      });
    });
    $(document).ready(function() {
      $('.tabs').tabs();
    });
  </script>

  <!-- Import materialize.js-->
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
</footer>

  • I don’t know much about Jquery. It didn’t work either, if you want to look at the page https://receitas-alpha.firebasepp.com

  • I tested changing this too but it didn’t work, what I find strange is reason to go to the top, the scroll script should only make scrolling slow

  • Try it like this : var $target = $(this).closest("li").is(".tab") ? $('a[href="'+target+'"]') : $(target);

  • Then switch to 'scrollTop': $(this).offset().top

  • Delete the lines var target = this.hash; and var $target = $(target);

  • It didn’t help either

  • So your problem is beyond the question. The solution only solves the tabs problem.

Show 2 more comments

0

I just removed the instruction that threw the page up and it looks like it worked. Try to do this.

<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/css/materialize.min.css" media="screen,projection">


<div style='margin-top: 500px' id='tudo' class="col s12">
  <ul class="tabs">
    <li class="tab col s4"><a class='active' href="#minhas">Minhas</a></li>
    <li class="tab col s4"><a href="#favoritas">Favoritas</a></li>
    <li class="tab col s4"><a href="#publicas">Públicas</a></li>
  </ul>

  <!-- LISTA DE MINHAS -->
  <div class="row" id="minhas"></div>

  <!-- LISTA FAVORITAS -->
  <div class="row" id="favoritas"></div>

  <!-- LISTA PÚBLICAS -->
  <div class="row" id="publicas"></div>
</div>

<footer>
  <!-- Import JQuery.js -->
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  <!-- Smooth scrolling effect -->
  <script>
    $(document).scroll(function() {
      $('a[href^="#"]').on('click', function(e) {
        e.preventDefault();

        var target = this.hash;
        var $target = $(target);

        // Removi a linha que vinha aqui
      });
    });
    $(document).ready(function() {
      $('.tabs').tabs();
    });
  </script>

  <!-- Import materialize.js-->
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-beta/js/materialize.min.js"></script>
</footer>

  • 1

    This line is the smooth scrolling effect that cannot be removed because it is used in other parts of html

0


What was causing the problem was the tabs initialization

$('.tabs').tabs();

I was able to solve the problem by adding a class to the links that are not part of the tabs and changing the smooth scroll effect selector to

$('a[href^="#"].link')...

So the effect is only called on the links that have the class link

  • I didn’t think the existence of other links would interfere

  • I’m sorry for anything, but I value your answer (so much that I gave +1) and you helped me by removing parts of the scroll effect that I didn’t even need, while you were looking for the solution I was also doing some more tests from what you said and I ended up discovering what caused the error, after I got this idea to add a class, I think it’s kind of Gambi, and so I still want to leave the question open to see if anyone can figure out how to solve the problem without having to add an extra class to all the anchor links

  • Quiet, if you find a way to solve the problem less gambiarrosa warns there :)

  • Actually I didn’t think gambiarra no. You just segmented the links that interested you to the case. I think it’s correct.

  • Having to add a class to all anchor items for the smooth scrolling effect to work, even if it is not a Gambi, is at least one hand

Browser other questions tagged

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