sidebar.vue 3.5 KB

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