zoukankan      html  css  js  c++  java
  • 关于vue项目中在js中引入图片问题

    <template>
    <div>
    <img v-for="(star,index) in stars" :src="star.src" @click="rating(index)" alt="星星图片"/>
    </div>
    </template>

    <script>
    //星星的图片路径
    // var starOffImg = "../../../static/images/OrderCenter/pinglun/wmwjx.png";
    // var starOnImg = "../../../static/images/OrderCenter/pinglun/mwjx.png";

    import starOffImg from '../../../static/images/OrderCenter/pinglun/wmwjx.png'; // import 引入图片
    import starOnImg from '../../../static/images/OrderCenter/pinglun/mwjx.png'; // import 引入图片

    export default {
    data() {
    return {
    stars: [
    {
    src: starOffImg,
    active: false
    },
    {
    src: starOffImg,
    active: false
    },
    {
    src: starOffImg,
    active: false
    },
    {
    src: starOffImg,
    active: false
    },
    {
    src: starOffImg,
    active: false
    }
    ],
    };
    },
    methods: {
    //评分
    rating: function (index) {
    var total = this.stars.length; //星星总数
    var idx = index + 1; //这代表选的第idx颗星-也代表应该显示的星星数量

    //如果再次点击当前选中的星级-仅取消掉当前星级,保留之前的。
    if (idx == this.starNum) {
    for (var i = index; i < total; i++) {
    this.stars[i].src = starOffImg;
    this.stars[i].active = false;
    }
    }
    //如果小于当前最高星级,则直接保留当前星级
    if (idx < this.starNum) {
    for (var i = idx; i < this.starNum; i++) {
    this.stars[i].src = starOffImg;
    this.stars[i].active = false;
    }
    }
    //如果大于当前星级,则直接选到该星级
    if (idx > this.starNum) {
    for (var i = 0; i < idx; i++) {
    this.stars[i].src = starOnImg;
    this.stars[i].active = true;
    }
    }

    var count = 0; //计数器-统计当前有几颗星
    for (var i = 0; i < total; i++) {
    if (this.stars[i].active) {
    count++;
    }
    }
    this.starNum = count;
    }
    },


    }
    </script>

    <style scoped>

    img{
    34px;
    height: 32px;
    margin-left: 32px;
    }
    </style>

  • 相关阅读:
    各种blog尝试后,发现wordpress适用起来最方便
    索引器(C# 编程指南)
    vue3 父子组件之间的传值
    vue3 + vite + ts 搭建项目
    vue3中使用全局变量
    用JSONKit库解析json文件
    程序内部让用户直接上appstore评价游戏的链接地址以及跳转方法
    常见错误:Apple MachO Linker Error
    常见错误:多线程界面元素显示错误
    常见错误:Undefined symbols for architecture i386
  • 原文地址:https://www.cnblogs.com/WangXinPeng/p/9413530.html
Copyright © 2011-2022 走看看