锚点 与 scrollIntoView()

一、锚点

  <p id="firstAnchor"> 锚点 </p>

  <a href="#firstAnchor"> 跳转 </a>

  js 控制

   1、location.href = "#firstAnchor";      // firstAnchor为锚点名称  

   2、window.location.hash = "#firstAnchor"; // firstAnchor为锚点名称

    经测试:hash只会在跳转到此页面的第一次起作用,再次刷新此页面将不起作用,而href始终起作用

二、scrollIntoView()

 参考:scrollIntoView()
 可以用来实现锚点效果:
  document.getElementById("#firstAnchor").scrollIntoView(true);
 详细介绍API https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
  

三、vue组件 和jQuery插件

  vue :vue-scrollto 待续;

  jQuery: 等待高手补充

 

posted @ 2017-10-11 11:36  半天的半天  阅读(2627)  评论(0编辑  收藏  举报