Skip to main content
Active reading [<https://en.wikipedia.org/wiki/Vue.js> <https://en.wikipedia.org/wiki/Cascading_Style_Sheets> <https://en.wikipedia.org/wiki/HTML5>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 109
  • 132

a vue2A Vue.js 2 solution ... add a 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"

 /* smoothSmooth scrolling in cssCSS - it works in html5HTML5 only */
 html, body {
     scroll-behavior: smooth;
 }

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;
 }

A Vue.js 2 solution ... add a 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 - it works in HTML5 only */
 html, body {
     scroll-behavior: smooth;
 }
fixed bug
Source Link
Yordan Georgiev
  • 5.3k
  • 2
  • 59
  • 55

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 = "foo" //force the updateapp.updater call= 

 "page_load"

 /* smooth scrolling in css - works in html5 only */
 html, body {
     scroll-behavior: smooth;
 }

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 ) {
           location.href = "#"+String(anchor)
        }
     })
  }

 

app.updater = "foo" //force the update call 

 

 /* smooth scrolling in css - works in html5 only */
 html, body {
     scroll-behavior: smooth;
 }

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;
 }
replace with vue2 solution
Source Link
Yordan Georgiev
  • 5.3k
  • 2
  • 59
  • 55

a slightly more complete version of the Michael Whinfrey's answer , a pure JavaScriptvue2 solution, which will make the the jump to anchor on every page load ( aka also when landing from another page ) ... add simple data property to simply force the update

function scrollTo(name) {
const app scrollToResolver= new Vue(document{ .getElementById(name));.. 
}

function scrollToResolver , updated: function(elem) {
  var jump = parseInt(elemthis.getBoundingClientRect$nextTick(function().top * .2);{
  document.body.scrollTop += jump;
  document  var uri = window.documentElementlocation.scrollTophref
 += jump;
  if (!elem.lastjump || elem.lastjump >var Mathanchor = ( uri.absindexOf(jump)'#') {
=== -1 ) ? elem.lastjump'' =: Mathuri.abssplit(jump'#');[1]
    setTimeout(function() { scrollToResolver  if (elem);}, "20"String(anchor);
.length > }0 else) {
    elem.lastjump = null;
  }
}

window   location.onloadhref = function WindowLoad"#"+String(eventanchor) {
   var uri = window.location.href  }
   var anchor =})
 ( uri}



app.indexOf('#')updater ==== -1"foo" )//force ?the ''update :call uri.split('#')[1] 



 /* smooth ifscrolling (in anchorcss !=- ''works )in {html5 window.location.hashonly =*/
 anchorhtml, }body {
   scrollTo(anchor)  scroll-behavior: smooth;
 }

a slightly more complete version of the Michael Whinfrey's answer , a pure JavaScript solution, which will make the the jump to anchor on every page load ( aka also when landing from another page ) ...

function scrollTo(name) {
  scrollToResolver(document.getElementById(name));
}

function scrollToResolver(elem) {
  var jump = parseInt(elem.getBoundingClientRect().top * .2);
  document.body.scrollTop += jump;
  document.documentElement.scrollTop += jump;
  if (!elem.lastjump || elem.lastjump > Math.abs(jump)) {
    elem.lastjump = Math.abs(jump);
    setTimeout(function() { scrollToResolver(elem);}, "20");
  } else {
    elem.lastjump = null;
  }
}

window.onload = function WindowLoad(event) {
   var uri = window.location.href
   var anchor = ( uri.indexOf('#') === -1 ) ? '' : uri.split('#')[1]
   if ( anchor != '' ) { window.location.hash = anchor }
   scrollTo(anchor)
}

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 ) {
           location.href = "#"+String(anchor)
        }
     })
  }



app.updater = "foo" //force the update call  



 /* smooth scrolling in css - works in html5 only */
 html, body {
     scroll-behavior: smooth;
 }
Source Link
Yordan Georgiev
  • 5.3k
  • 2
  • 59
  • 55
Loading