• 页面滚动那些事儿(滚动条样式自定义,隐藏滚动条,scrollIntoView遇到头部fixed定位滚动被遮挡、vue-scrollto插件的应用)


    1、滚动条自定义样式

            1-1、设定全局样式

                    当页面存在多个滚动条时,想要所有的滚动条设置成同一个样式可以:

                    以谷歌浏览器举例,因滚动条的设置存在兼容问题,下方有其他浏览器样式配置

    1. /*滚动条样式*/
    2. ::-webkit-scrollbar {
    3. background: #eee;
    4. width: 8px;
    5. }
    6. /*滚动条轨道的样式*/
    7. ::-webkit-scrollbar-track {
    8. background: coral;
    9. width: 8px;
    10. }
    11. /*滚动条滑块的样式*/
    12. ::-webkit-scrollbar-thumb {
    13. background: red;
    14. width: 8px;
    15. border-radius: 4px;
    16. }

                    代码实例演示:

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>Documenttitle>
    8. <style>
    9. ::-webkit-scrollbar {
    10. background: coral;
    11. width: 8px;
    12. }
    13. ::-webkit-scrollbar-thumb {
    14. background: yellow;
    15. width: 8px;
    16. border-radius: 4px;
    17. }
    18. .scrollbox {
    19. margin: 0 auto;
    20. width: 640px;
    21. height: 480px;
    22. overflow: auto;
    23. line-height: 100px;
    24. text-align: center;
    25. font-size: 38px;
    26. background-color: rgb(89, 191, 238);
    27. }
    28. style>
    29. head>
    30. <body>
    31. <div class="scrollbox ">
    32. <div class="title">xdiv>
    33. <div class="title">idiv>
    34. <div class="title">bdiv>
    35. <div class="title">idiv>
    36. <div class="title">ndiv>
    37. <div class="title">gdiv>
    38. <div class="title">-div>
    39. <div class="title">Gdiv>
    40. div>
    41. <br />
    42. <div class="scrollbox ">
    43. <div class="title">xdiv>
    44. <div class="title">idiv>
    45. <div class="title">bdiv>
    46. <div class="title">idiv>
    47. <div class="title">ndiv>
    48. <div class="title">gdiv>
    49. <div class="title">-div>
    50. <div class="title">Gdiv>
    51. div>
    52. body>
    53. html>

             1-2、设定局部样式

                    当页面存在多个滚动条时,想要不同的滚动条设置成不同的样式

                    可以对不同的【滚动区域结构】设置不同的-webkit-(scrollbar、scrollbar-thumb)

                    代码实例演示:

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>Documenttitle>
    8. <style>
    9. .scrollbox {
    10. margin: 0 auto;
    11. width: 640px;
    12. height: 480px;
    13. overflow: auto;
    14. line-height: 100px;
    15. text-align: center;
    16. font-size: 38px;
    17. background-color: rgb(89, 191, 238);
    18. }
    19. .scro1::-webkit-scrollbar {
    20. background: coral;
    21. width: 8px;
    22. }
    23. .scro1::-webkit-scrollbar-thumb {
    24. background: yellow;
    25. width: 8px;
    26. border-radius: 4px;
    27. }
    28. .scro2::-webkit-scrollbar {
    29. background: orange;
    30. width: 18px;
    31. border-radius: 9px;
    32. }
    33. .scro2::-webkit-scrollbar-thumb {
    34. width: 18px;
    35. border-radius: 9px;
    36. background: green;
    37. }
    38. style>
    39. head>
    40. <body>
    41. <div class="scrollbox scro1">
    42. <div class="title">xdiv>
    43. <div class="title">idiv>
    44. <div class="title">bdiv>
    45. <div class="title">idiv>
    46. <div class="title">ndiv>
    47. <div class="title">gdiv>
    48. <div class="title">-div>
    49. <div class="title">Gdiv>
    50. div>
    51. <br />
    52. <div class="scrollbox scro2">
    53. <div class="title">xdiv>
    54. <div class="title">idiv>
    55. <div class="title">bdiv>
    56. <div class="title">idiv>
    57. <div class="title">ndiv>
    58. <div class="title">gdiv>
    59. <div class="title">-div>
    60. <div class="title">Gdiv>
    61. div>
    62. body>
    63. html>

             1-3、提示:

                    背景色也可以设置为transparent透明

                    不同浏览器对于滚动条的样式兼容不同

    1. /*IE浏览器⬇*/
    2. /*三角箭头的颜色*/
    3. scrollbar-arrow-color:;
    4. /*立体滚动条的颜色*/
    5. scrollbar-face-color:;
    6. /*立体滚动条亮边的颜色*/
    7. scrollbar-3dlight-color:;
    8. /*滚动条空白部分的颜色*/
    9. scrollbar-highlight-color:;
    10. /*立体滚动条阴影的颜色*/
    11. scrollbar-shadow-color:;
    12. /*立体滚动条强阴影的颜色*/
    13. scrollbar-darkshadow-color:;
    14. /*立体滚动条背景颜色*/
    15. scrollbar-track-color:;
    16. /*滚动条的基本颜色*/
    17. scrollbar-base-color:;
    18. /*火狐浏览器⬇*/
    19. /*滚动条滑块、轨道的颜色设置*/
    20. scrollbar-color: coral yellow;
    21. /*滚动条宽度 auto thin none*/
    22. scrollbar-width: ;

    2、隐藏滚动条

            (不少同学想要滚动,但不想要滚动条,滚动条说:就多了一个字我就变丑了?)

            2-1、普通滚动条的隐藏--就类似于上面那种普通的滚动条

                    【注意】滚动条隐藏的选择器,谷歌写在-webkit-scrollbar中,火狐IE写在滚动标签下

    1. .demo::-webkit-scrollbar {
    2. display: none; /* Chrome Safari */
    3. }
    4. .demo {
    5. scrollbar-width: none; /* firefox /
    6. -ms-overflow-style: none; / IE 10+ */
    7. overflow-y: auto;
    8. }

                    代码实例演示: 

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>Documenttitle>
    8. <style>
    9. .scrollbox {
    10. margin: 0 auto;
    11. width: 640px;
    12. height: 480px;
    13. overflow: auto;
    14. line-height: 100px;
    15. text-align: center;
    16. font-size: 38px;
    17. background-color: rgb(89, 191, 238);
    18. }
    19. .scro1::-webkit-scrollbar {
    20. display: none;/* Google */
    21. }
    22. .scro1 {
    23. scrollbar-width: none; /* firefox */
    24. -ms-overflow-style: none; /* IE 10+ */
    25. }
    26. style>
    27. head>
    28. <body>
    29. <div class="scrollbox scro1">
    30. <div class="title">xdiv>
    31. <div class="title">idiv>
    32. <div class="title">bdiv>
    33. <div class="title">idiv>
    34. <div class="title">ndiv>
    35. <div class="title">gdiv>
    36. <div class="title">-div>
    37. <div class="title">Gdiv>
    38. div>
    39. body>
    40. html>

             2-2、iframe的滚动条隐藏(视觉隐藏)

                    借助位置偏移,父级宽度小于iframe的宽度,来遮挡住iframe的滚动条

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <title>Titletitle>
    6. <style>
    7. #box {
    8. width: 530px;
    9. height: 680px;
    10. overflow: hidden;
    11. }
    12. iframe {
    13. width: 530px;
    14. height: 100%;
    15. }
    16. style>
    17. head>
    18. <body>
    19. <div id="box">
    20. <iframe
    21. marginwidth="0"
    22. marginheight="0"
    23. src="transitiondemo.html"
    24. frameborder="0"
    25. >iframe>
    26. div>
    27. body>
    28. html>

    3、锚点定位滚动之 scrollIntoView、vue-scrollto

            3-1 源自于element的接口scrollIntoView会滚动元素的父容器

                    问题:当顶部出现fixed固定定位时,滚动区域就会向上顶,也就会被顶部遮住一部分

                    解决:让滚动区也以fixed做呈现,top值定为顶部区域的高度即可不被遮挡;

    1. <template>
    2. <div class="scroll-box">
    3. <div class="header">here is headerdiv>
    4. <aside>
    5. <li
    6. v-for="(item, index) in asideItem"
    7. :key="item"
    8. @click="scrollTo(index)"
    9. >
    10. {{ item }}
    11. li>
    12. aside>
    13. <div class="content">
    14. <div class="con-item" v-for="item in asideItem" :key="item">
    15. {{ item }}
    16. div>
    17. div>
    18. div>
    19. template>
    20. <script>
    21. export default {
    22. name: 'scrollIntoView',
    23. data() {
    24. return {
    25. asideItem: ['google', 'uc', 'IE', 'firefox', '360', 'liebao']
    26. };
    27. },
    28. methods: {
    29. scrollTo(index) {
    30. document.querySelectorAll('.con-item')[index].scrollIntoView({
    31. behavior: 'smooth',
    32. block: 'start',
    33. inline: 'nearest'
    34. });
    35. }
    36. }
    37. };
    38. script>
    39. <style lang="less">
    40. .scroll-box {
    41. width: 100%;
    42. height: 100%;
    43. }
    44. .header {
    45. position: fixed;
    46. top: 0;
    47. left: 0;
    48. width: 100%;
    49. height: 100px;
    50. line-height: 100px;
    51. font-size: 36px;
    52. background: coral;
    53. opacity: 0.6;
    54. }
    55. aside {
    56. position: fixed;
    57. right: 0;
    58. bottom: 100px;
    59. width: 80px;
    60. height: 200px;
    61. background: yellow;
    62. & > li:hover {
    63. cursor: pointer;
    64. }
    65. /*因为content给与固定定位,为了让侧边栏不被遮盖得提高层级*/
    66. z-index: 999;
    67. }
    68. .content {
    69. width: 100%;
    70. height: 750px;
    71. overflow-y: scroll;
    72. border: 2px dashed darksalmon;
    73. margin: 0 auto;
    74. position: fixed;
    75. top: 100px;
    76. overflow-y: scroll;
    77. & > div {
    78. width: 100%;
    79. height: 500px;
    80. line-height: 500px;
    81. text-align: center;
    82. font-size: 36px;
    83. }
    84. & > div:nth-child(2n) {
    85. background: burlywood;
    86. }
    87. /*不显示滚动条——上文有介绍了*/
    88. &::-webkit-scrollbar {
    89. display: none; /* Chrome Safari */
    90. }
    91. }
    92. li {
    93. line-height: 30px;
    94. }
    95. style>

            3-2 vue适用的vue-scrollto插件实现

                    两种配置方式(全局配置、局部配置)

                    问题:全局配置这里我这边测试时offset配置项不太好使(也可能我忽视了什么细节)

                    先安装功能依赖,npm包位置

    1. ## npm安装
    2. npm install --save vue-scrollto
    3. ## yarn安装
    4. yarn add vue-scrollto

                    看全局配置

    1. import Vue from 'vue';
    2. import App from './App.vue';
    3. const VueScrollTo = require('vue-scrollto');
    4. Vue.use(VueScrollTo, {
    5. container: 'document', // 滚动元素
    6. duration: 500, // 动画时长
    7. easing: 'ease', // 动画曲线
    8. offset: 100, // 滚动终点距离父元素顶部距离
    9. force: true, // 强制立即执行,即便元素是可见的
    10. cancelable: true,
    11. onStart: false,
    12. onDone: false,
    13. onCancel: false,
    14. x: false, // x 轴禁止滚动
    15. y: true
    16. });
    17. new Vue({
    18. render: function(h) {
    19. return h(App);
    20. }
    21. }).$mount('#app1');
    this.$scrollTo(element, duration, options)

                    还可以通过v-指令来实现滚动(v-scroll-to)

    1. <template>
    2. <div id="app">
    3. <div class="flex-box">
    4. <div class="flex-l">
    5. <ul class="jumpbtn">
    6. <li class="btn" v-for="item in boxs" :key="item.id">
    7. <a href="#" v-scroll-to="'#' + item.id">
    8. {{ item.id + item.title }}
    9. >
    10. li>
    11. ul>
    12. div>
    13. <div class="flex-r">
    14. <scroll-page
    15. v-for="item in boxs"
    16. :key="item.id"
    17. :id="item.id"
    18. :title="item.id + item.title"
    19. />
    20. div>
    21. div>
    22. div>
    23. template>
    24. <script>
    25. export default {
    26. name: "App",
    27. data() {
    28. return {
    29. boxs: [
    30. { id: "bt-1", title: "Anita" },
    31. { id: "bt-2", title: "Anita" },
    32. { id: "bt-3", title: "Anita" },
    33. { id: "bt-4", title: "Anita" },
    34. { id: "bt-5", title: "Anita" },
    35. { id: "bt-6", title: "Anita" },
    36. { id: "bt-7", title: "Anita" },
    37. ],
    38. };
    39. },
    40. };
    41. script>
    42. <style>
    43. a {
    44. text-decoration: none;
    45. }
    46. li {
    47. list-style: none;
    48. }
    49. #app {
    50. font-family: "仿宋";
    51. -webkit-font-smoothing: antialiased;
    52. -moz-osx-font-smoothing: grayscale;
    53. text-align: center;
    54. color: #2c3e50;
    55. margin-top: 60px;
    56. }
    57. .flex-box {
    58. display: flex;
    59. flex-direction: row;
    60. border: 1px solid salmon;
    61. }
    62. .flex-l {
    63. width: 20%;
    64. position: fixed;
    65. top: 100px;
    66. right: 0px;
    67. border: 1px dashed yellowgreen;
    68. }
    69. .flex-r {
    70. width: 80%;
    71. }
    72. style>

                    局部配置使用(offset的配置就可以来解决滚动至顶部被fixed头部遮盖的问题)

    1. <template>
    2. <div class="scrollDemo">
    3. <div class="demoNav flex-center-center">
    4. <div
    5. class="demoNavItem"
    6. v-for="(item, index) in demoNavItem"
    7. :key="index"
    8. :class="{ navActive: idx == index }"
    9. @click="changNav(index)"
    10. >
    11. {{ item }}
    12. div>
    13. div>
    14. <div class="demoContent">
    15. <div class="demoItem0 demoItem" id="demoItem0">googlediv>
    16. <div class="demoItem1 demoItem" id="demoItem1">ucdiv>
    17. <div class="demoItem2 demoItem" id="demoItem2">IEdiv>
    18. <div class="demoItem3 demoItem" id="demoItem3">firefoxdiv>
    19. <div class="demoItem4 demoItem" id="demoItem4">360div>
    20. <div class="demoItem5 demoItem" id="demoItem5">liebaodiv>
    21. div>
    22. div>
    23. template>
    24. <script>
    25. // 引入
    26. var VueScrollTo = require("vue-scrollto");
    27. export default {
    28. data() {
    29. return {
    30. idx: 0,
    31. demoNavItem: [
    32. "google",
    33. "uc",
    34. "IE",
    35. "firefox",
    36. "360",
    37. "liebao",
    38. ],
    39. };
    40. },
    41. methods: {
    42. // 点击导航后触发
    43. changNav(index) {
    44. console.log(this)
    45. this.idx = index;
    46. // 通过VueScrollTo调用内置scrollTo方法实现滚动效果
    47. VueScrollTo.scrollTo(document.getElementById("demoItem" + index), 1000, {
    48. offset: -70,
    49. });
    50. },
    51. },
    52. };
    53. script>
    54. <style scoped>
    55. .flex-center-center {
    56. display: flex;
    57. align-items: center;
    58. justify-content: center;
    59. }
    60. .demoNav {
    61. width: 100%;
    62. height: 70px;
    63. background: rgb(247, 183, 153);
    64. /* 浏览器窗口顶部 */
    65. position: fixed;
    66. /* position: sticky; */
    67. left: 0;
    68. top: 0;
    69. }
    70. .demoNavItem {
    71. font-size: 40px;
    72. color: #fff;
    73. margin-left: 30px;
    74. cursor: pointer;
    75. }
    76. .navActive {
    77. color: rgb(36, 39, 204);
    78. }
    79. .demoItem {
    80. width: 100%;
    81. height: 600px;
    82. font-size: 60px;
    83. color: #fff;
    84. text-align: center;
    85. padding: 60px 0 0 0;
    86. }
    87. .demoItem0 {
    88. background: rgba(255, 61, 61, 0.945);
    89. }
    90. .demoItem1 {
    91. background: rgb(250, 159, 40);
    92. }
    93. .demoItem2 {
    94. background: rgb(238, 255, 0);
    95. }
    96. .demoItem3 {
    97. background: rgb(63, 247, 7);
    98. }
    99. .demoItem4 {
    100. background: rgb(21, 114, 253);
    101. }
    102. .demoItem5 {
    103. background: rgb(248, 6, 248);
    104. }
    105. style>

     

  • 相关阅读:
    Vue路由介绍及使用
    【网络协议】聊聊DNS协议如何域名解析和负载均衡
    grid布局之项目属性grid-column&grid-row
    LSF集群基本概念介绍
    OpenMV与STM32之间的通信(附源码)
    Docker02基本管理
    在vs中创建linux项目
    基于内容的图像检索系统设计与实现--颜色信息--纹理信息--形状信息--PHASH--SHFT特征点的综合检测项目,包含简易版与完整版的源码及数据!
    记一次简单的js逆向分析
    Stearic acid-PEG-FITC 硬脂酸-聚乙二醇-荧光素
  • 原文地址:https://blog.csdn.net/qq_38800316/article/details/126675790