How to scroll HTML page to given anchor?

后端 未结 17 1957
醉梦人生
醉梦人生 2020-11-22 08:55

I’d like to make the browser to scroll the page to a given anchor, just by using JavaScript.

I have specified a name or id attribute in my

17条回答
  •  自闭症患者
    2020-11-22 09:13

    a vue2 solution ... add simple data property to simply force the update

      const app = new Vue({ 
      ... 
    
      , updated: function() {
               this.$nextTick(function() {
               var uri = window.location.href
               var anchor = ( uri.indexOf('#') === -1 ) ? '' : uri.split('#')[1]
               if ( String(anchor).length > 0 && this.updater === 'page_load' ) {
                  this.updater = "" // only on page-load !
                  location.href = "#"+String(anchor)
               }
             })
            }
         });
         app.updater = "page_load"
    
     /* smooth scrolling in css - works in html5 only */
     html, body {
         scroll-behavior: smooth;
     }
    

提交回复
热议问题