How do I make this div move to the top of the mobile screen? (code provided)

I don’t know why this script does not move the < li> element to the top of the mobile screen:

    function scrollup(){
      var div=document.getElementById('card1'); = '20'+'px'; = '70'+'px';


<li id="card1" onclick="scrollup();">
                      <div class="collapsible-header"><b>Chart: gearbox, diffs, arms</b></div>
                      <div class="collapsible-body" style="padding: 0 10px;">
                          <p>Best combination for each kit.</p>
                  <table class="striped">

I am using materializecss’ Collapsible feature, which is an accordion. When you tap on the .collapsible-header then the accordion opens and automatically shuts the accordion drawer above it. The problem I’m trying solve: if you tap on the section header, it expands with content that fills several mobile screens. When you tap the following header, the header scrolls up to meet the first header, which is several screens up off the screen, and you have to manually scroll back up to the beginning.

So I want the user to tap on any header and it gets positioned at the top of the screen, ready to read. Note that I put the code on the < li> tag. I had also put it on the .collapsible-header div and it had no effect there either. I also tried the following, which also did not work:

 = '20px';
 = '70px';

I also added the following, but it did not move the element to the top, but relative to the previous < li>: = 'relative';

Changing relative to absolute screws up the layout, overlaying the < li> on top of the table.

In some way I think I need to grab the left bottom of the upper div and slide the tapped < li> element so the entire < ul> element slides to where the chosen < li> butts up against it.

I wonder if window.scrollTo(0,0) is something to try, but I can’t figure out how to make it work in my case.

If this code is supposed to work as is, then perhaps the materializecss environment is working against it.

Collapsible Accordion reference:

Collapsible JS on Github to examine:

I coded the < li> element:

<li id="card1" onclick="scrollup();">


        function scrollup(alignToTop){
          var div=document.getElementById('card1');

This did not work either. The accordion drawer underneath continues to slide up so its beginning is off screen.

The next thing I did (and has no effect):

Made link to JqueryUI:

< script   src=""   integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="   crossorigin="anonymous"></script>

Added JS script:

        $(function() {
            autoHeight: false,
            collapsible: true,
            heightStyle: "content",
            active: 0,
            animate: 300
          $('#accordion li').bind('click',function() {
            var self = this;
            setTimeout(function() {
              theOffset = $(self).offset();
            }, 300);

Added the accordion id to the ul so it matches the script:

 <ul id="accordion" class="collapsible" data-collapsible="accordion">

The above code has no effect. It came from a fiddle at It’s 2 years old, so that may be the problem!


Do you mean something like this?

It uses the default files from material and the only extra is the js you mentioned in the fiddle.

            var self = this;
            setTimeout(function() {
                theOffset = $(self).offset();
                $('body,html').animate({ scrollTop: - 10 });
            }, 310);

I just changed the offset and targetted the correct element (.collapsible-header).

I’ve been experimenting with a very similar code as yours. I just did not have the right elements in the js. Wow, I’m so relieved to finally get this right. You are a life saver! I just changed the offset to clear the header and … FANTASTIC!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.