sidebar.vue 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <div class="client-sidebar">
  3. <ul>
  4. <li><a class="sidebar-default-box"><i></i><p>咨询</p></a></li>
  5. <li>
  6. <div class="sidebar-default-box"><i @click="backTop"></i><p>客服</p></div>
  7. <div class="sidebar-hover-box">
  8. <span>服务热线</span><h4>4000522130</h4>
  9. </div>
  10. </li>
  11. <li><a class="sidebar-default-box"><i></i><p>体验</p></a></li>
  12. <li v-if="btnFlag"><i @click="backTop"></i><p>置顶</p></li>
  13. </ul>
  14. </div>
  15. </template>
  16. <script>
  17. export default {
  18. name: 'mtaSidebar',
  19. // vue的两个生命钩子,这里不多解释。
  20. // window对象,所有浏览器都支持window对象。它表示浏览器窗口,监听滚动事件
  21. mounted () {
  22. window.addEventListener('scroll', this.scrollToTop)
  23. },
  24. destroyed () {
  25. window.removeEventListener('scroll', this.scrollToTop)
  26. },
  27. data(){
  28. return {
  29. btnFlag:false
  30. }
  31. },
  32. methods: {
  33. // 点击图片回到顶部方法,加计时器是为了过渡顺滑
  34. backTop() {
  35. const that = this
  36. let timer = setInterval(() => {
  37. let ispeed = Math.floor(-that.scrollTop / 5)
  38. document.documentElement.scrollTop = document.body.scrollTop = that.scrollTop + ispeed
  39. if (that.scrollTop === 0) {
  40. clearInterval(timer)
  41. }
  42. }, 16)
  43. },
  44. // 为了计算距离顶部的高度,当高度大于60显示回顶部图标,小于60则隐藏
  45. scrollToTop() {
  46. const that = this
  47. let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
  48. that.scrollTop = scrollTop
  49. if (that.scrollTop > 60) {
  50. that.btnFlag = true
  51. } else {
  52. that.btnFlag = false
  53. }
  54. }
  55. }
  56. };
  57. </script>
  58. <style lang="scss" scoped>
  59. .client-sidebar{
  60. display: none;
  61. position: fixed;
  62. z-index: 100;
  63. top: 300px;
  64. right: 0;
  65. li{
  66. width: 94px;height: 102px;position: relative;
  67. i{width: 45px;height: 40px;display: block;background: #8be0ad;margin: 16px auto;}
  68. p{font-size:18px;color: #4c4c4c;text-align: center;}
  69. .sidebar-default-box{border: 1px solid #eee;box-sizing: border-box;background: #fff;z-index: 2;background: #fff;position: absolute;top:0;left: 0;right: 0;bottom: 0;}
  70. .sidebar-hover-box{
  71. width: 220px;height: 102px;display: block;padding: 24px;border: 1px solid #eee;transition: all 0.5s;
  72. position: absolute;right: -150px;background: #fff;top: 0;box-sizing: border-box;z-index: 1;
  73. span{font-size:16px;display: block;color: #1f1f1f;margin-bottom: 10px}
  74. h4{font-size:30px;color: #3e7ee7;font-weight: bold;}
  75. }
  76. }
  77. li:hover{
  78. .sidebar-default-box{background: #5da8f7;}
  79. .sidebar-hover-box{
  80. right: 92px;
  81. }
  82. }
  83. li:nth-child(1) i{-background: pink;}
  84. li:nth-child(2) i{-background: green;}
  85. }
  86. </style>