栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Vue.js

Vue造轮子-popover组件(下)

Vue.js 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Vue造轮子-popover组件(下)


title: Vue造轮子-popover组件(下)
date: 2020-01-29 10:37:44
tags: 前端学习 造轮子 1. 上一次的问题总结。
  • overflow:hidden -> body.appendChild
  • 关闭重复 -> 分开,document只管外面,popover只管里面
  • 忘了取消监听document -> 收拢close
2. 可以把一个函数哟没有五行作为一个优化的标准,简称为五行定律 3. 接下来把样式改好点
  .content-wrapper { // 如果写了scoped,popover里面那么就只作用于popover里面,移到外面就在外面了就可以
    position: absolute;
    border: 1px solid  $border-color;
    border-radius: $border-radius;
    filter: drop-shadow(0 0 1px rgba(0,0,0,0.5));
    
    
    background: white;
    transform: translateY(-100%);
    margin-top: -10px;
    padding: .5em 1em;
    max-width: 20em;
    word-break: break-all;
    &::before ,  &::after{
      content: '';
      display: block;
      width: 0;
      height: 0;
      position: absolute;
      border: 10px solid transparent;
      left: 10px;
    }
    &::after{
      content: '';
      display: block;
      width: 0;
      height: 0;
      position: absolute;
      border: 10px solid transparent;
      left: 10px;
    }
    &::before {
      border-top-color: black;
      top: 100%;
    }
    &::after {
      border-top-color: white;
      top: calc(100% - 1px);
    }
  }
4. 增加功能:用户可以选择popover是上面下面左面右面
  // 先要给contentWrapper加一个类,传递进入位置信息
  
    

  // 获得位置信息     
  props: {
      position:{
 type: String,
 default: 'top',
 validator(value){
   return ['top','bottom','left','right'].indexOf(value) >= 0
 }
      }
    },
  }

  // 在method里面写方法,主要控制大概位置
  method: {
    positionContent() {
 const {contentWrapper, triggerWrapper} = this['$refs']
 document.body.appendChild(contentWrapper)
 let {width, height, top, left} = triggerWrapper.getBoundingClientRect()
 if(this.position === 'top'){
   contentWrapper.style.left = left + window.scrollX + "px"
   contentWrapper.style.top = top + window.scrollY + "px"
 }else if(this.position === 'bottom'){
   contentWrapper.style.left = left + window.scrollX + "px"
   contentWrapper.style.top = top + height + window.scrollY + "px"
 }else if(this.position === 'left'){
   contentWrapper.style.left = left + window.scrollX + "px"
   let {height: height2} = contentWrapper.getBoundingClientRect()
   contentWrapper.style.top = top  + window.scrollY + (height - height2)/2 + "px"
 }else if(this.position === 'right'){
   contentWrapper.style.left = left + window.scrollX + width + "px"
   let {height: height2} = contentWrapper.getBoundingClientRect()
   contentWrapper.style.top = top  + window.scrollY + (height - height2)/2 + "px"
 }
      },
  }

  // 然后对css进行处理,主要是箭头当想
  
  $border-color: #333;
  $border-radius: 4px;
  .popover {
    display: inline-block;
    vertical-align: top;
    position: relative;
  }
  .content-wrapper { // 如果写了scoped,popover里面那么就只作用于popover里面,移到外面就在外面了就可以
    position: absolute;
    border: 1px solid  $border-color;
    border-radius: $border-radius;
    filter: drop-shadow(0 0 1px rgba(0,0,0,0.5));
    
    
    background: white;
    padding: .5em 1em;
    max-width: 20em;
    word-break: break-all;
    &::before ,  &::after{
      content: '';
      display: block;
      border: 10px solid transparent;
      width: 0;
      height: 0;
      position: absolute;
    }

    &.position-top {
      transform: translateY(-100%);
      margin-top: -10px;
      &::before, &::after{
 left: 10px;
      }
      &::before {
 border-top-color: black;
 top: 100%;
      }
      &::after {
 border-top-color: white;
 top: calc(100% - 1px);
      }
    }

    &.position-bottom {
      margin-top: 10px;
      &::before, &::after{
 left: 10px;
      }
      &::before {
 border-bottom-color: black;
 bottom: 100%;
      }
      &::after {
 border-bottom-color: white;
 bottom: calc(100% - 1px);
      }
    }

    &.position-left{
      transform: translateX(-100%);
      margin-left: -10px;
      &::before, &::after{
 transform: translateY(-50%);
 top: 50%;
      }
      &::before {
 border-left-color: black;
 left: 100%;
      }
      &::after {
 border-left-color: white;
 left: calc(100% - 1px)
      }
    }

    &.position-right{
      margin-left: 10px;
      &::before, &::after{
 transform: translateY(-50%);
 top: 50%;
      }
      &::before {
 border-right-color: black;
 right: 100%;
      }
      &::after {
 border-right-color: white;
 right: calc(100% - 1px)
      }
    }

  }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/240604.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号