Blame view

src/components/NoticeBar/index.vue 3.84 KB
任超 committed
1
<template>
任超 committed
2 3 4 5 6 7 8
  <transition name="fade">
    <!--主要内容-->
    <div class="noticebar">
      <div style="margin-left:5px"></div>
      <svg-icon icon-class='notice' />
      <div style="margin-right:5px"></div>
      <div ref="back" class="back">
任超 committed
9
        <div ref="text" class="text" @mouseover="mouseOver" @mouseleave="mouseLeave">
任超 committed
10 11 12 13
          <p v-for="(item, index) in noticeList" :key="index">
            <span> {{ item.noticeTitle }}</span>
            <span class="noticePublishTime">{{ item.noticePublishTime }}</span>
            <el-button type="text" @click="handleNotice(item)" style="color:#F56C6C;font-size: 14px;">[点击查看]</el-button>
任超 committed
14 15
          </p>
        </div>
任超 committed
16 17
      </div>
    </div>
任超 committed
18
  </transition>
任超 committed
19 20 21 22
</template>
<script>
export default {
  props: {
任超 committed
23 24 25
    noticeList: {
      type: Array,
      default: []
任超 committed
26 27 28 29
    }
  },
  data () {
    return {
任超 committed
30 31 32 33 34 35 36 37
      speed: 50, // 速度(单位px/s)
      backWidth: '', // 父级宽度
      backHeight: '', // 父级高度
      wordLength: '', // 文本长度
      state: 1,
      firstAnimationTime: '', // 状态一动画效果
      secondAnimationTime: '', // 状态二动画效果
    };
任超 committed
38
  },
任超 committed
39 40 41 42 43 44 45 46 47 48 49 50
  watch: {
    noticeList: {
      handler (newName, oldName) {
        let that = this
        this.Listener();
        setTimeout(res => {
          that.getData();
        }, 100);
      },
      deep: true
    }
  },
任超 committed
51
  methods: {
任超 committed
52 53 54
    handleNotice (item) {
      this.$alertMes(item.noticeTitle, item.noticeContent)
    },
任超 committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    // 获取数据
    getData () {
      let style = document.styleSheets[0];
      let text = this.$refs.text;
      let back = this.$refs.back;
      this.backWidth = back.offsetWidth;
      this.backHeight = back.offsetHeight;
      text.style.lineHeight = this.backHeight + 'px';
      this.wordLength = text.offsetWidth;
      this.ComputationTime(); // 计算时间
      style.insertRule(
        `@keyframes firstAnimation {0%   {left:0px;}100%  {left:-${this.wordLength}px;}}`
      );
      style.insertRule(
        `@keyframes secondAnimation {0%   {left:${this.backWidth}px;}100%  {left:-${this.wordLength}px;}}`
      );
      setTimeout(res => {
        this.changeState();
任超 committed
73
      }, 300);
任超 committed
74
    },
任超 committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    // 用速度计算时间(想要保持速度一样,2种状态时间不同需算出)
    ComputationTime () {
      this.firstAnimationTime = this.wordLength / this.speed;
      this.secondAnimationTime =
        (this.wordLength + this.backWidth) / this.speed;
    },
    // 根据状态切换动画
    changeState () {
      let text = this.$refs.text;
      if (this.state == 1) {
        text.style.animation = `firstAnimation ${this.firstAnimationTime}s linear`;
        this.state = 2;
      } else {
        text.style.animation = `secondAnimation ${this.secondAnimationTime}s linear infinite`;
      }
    },
    Listener () {
      let text = this.$refs.text;
      text.addEventListener(
        'animationend',
        res => {
          this.changeState();
        },
        false
      )
    },
    mouseOver () {
      let text = this.$refs.text;
      text.style.animationPlayState = 'paused'
    },
    mouseLeave () {
      let text = this.$refs.text;
      text.style.animationPlayState = ''
任超 committed
108 109
    }
  }
任超 committed
110
};
任超 committed
111 112
</script>
<style lang="scss" scoped>
任超 committed
113 114 115
.noticebar {
  display: flex;
  align-items: center;
任超 committed
116
  width: 100%;
任超 committed
117
  height: 28px;
任超 committed
118
  background: rgba(0, 0, 0, 0.1);
任超 committed
119

任超 committed
120 121 122 123 124 125 126 127 128
  .icon {
    img {
      height: 100%;
      width: 100%;
    }
  }

  .back {
    overflow: hidden;
任超 committed
129
    white-space: nowrap;
任超 committed
130 131 132 133 134
    margin: 0 auto;
    height: 100%;
    width: 100%;
    cursor: pointer;
    position: relative;
任超 committed
135 136
    font-size: 14px;
    color: #fff;
任超 committed
137

任超 committed
138 139
    .text {
      position: absolute;
任超 committed
140
      display: inline-block;
任超 committed
141
      padding: 2px 0;
任超 committed
142 143 144
      display: flex;

      p {
任超 committed
145 146 147 148 149
        margin-right: 80px;
        display: flex;
        align-items: center;
        height: 28px;
        line-height: 28px;
任超 committed
150
      }
任超 committed
151 152 153 154
    }
  }
}
</style>