实现回到顶部功能,使用锚点定位方式回到页面顶部,出现了路由被替换的情况

    1. <a href="" @click.prevent="custormAnchor('brand')">品牌</a>
    2. methods: {
    3. custormAnchor(anchorName) {
    4. // 找到锚点
    5. let anchorElement = document.getElementById(anchorName);
    6. // 如果对应id的锚点存在,就跳转到锚点
    7. if(anchorElement) { anchorElement.scrollIntoView(); }
    8. }
    9. },

    一份长见识的记录:(纯css回到顶部)

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8" />
    5. <title>Flexbox 0 starting code</title>
    6. <style>
    7. .tab label {
    8. padding: 10px;
    9. border: 1px solid #ccc;
    10. margin-right: -1px;
    11. text-align: center;
    12. float: left;
    13. overflow: hidden;
    14. }
    15. .tab::after {
    16. content: "";
    17. display: table;
    18. clear: both;
    19. }
    20. .box {
    21. height: 200px;
    22. border: 1px solid #ccc;
    23. scroll-behavior: smooth;
    24. overflow: hidden;
    25. margin-top: 10px;
    26. }
    27. .item {
    28. height: 100%;
    29. position: relative;
    30. overflow: hidden;
    31. }
    32. .item input {
    33. position: absolute;
    34. top: 0;
    35. height: 100%;
    36. width: 1px;
    37. border: 0;
    38. padding: 0;
    39. margin: 0;
    40. clip: rect(0 0 0 0);
    41. }
    42. </style>
    43. </head>
    44. <body>
    45. <h1>纯CSS选项卡</h1>
    46. <div class="tab">
    47. <label for="tab1">选项卡1</label>
    48. <label for="tab2">选项卡2</label>
    49. <label for="tab3">选项卡3</label>
    50. </div>
    51. <div class="box">
    52. <div class="item">
    53. <input type="text" id="tab1">
    54. <p>选项卡1内容</p>
    55. </div>
    56. <div class="item">
    57. <input type="text" id="tab2">
    58. <p>选项卡2内容</p>
    59. </div>
    60. <div class="item">
    61. <input type="text" id="tab3">
    62. <p>选项卡3内容</p>
    63. </div>
    64. </div>
    65. </body>
    66. </html>

    查阅大量博文,该方法不需要操作dom,原理后面要补~

    1. <div class="box" @scroll="scrollEvent">
    2. <input type="text" />
    3. <ul>
    4. <li v-for="(item, index) in imageGruop1" :key="index">
    5. <div>
    6. <img :src="item" alt="" />
    7. </div>
    8. </li>
    9. </ul>
    10. </div>
    11. methods: {
    12. scrollEvent(e) {
    13. //相等于节点获取元素
    14. this.div = e.srcElement
    15. },
    16. //绑在tabs的事件
    17. nameFunction() {
    18. this.div.scrollTop = 0
    19. },
    20. },