样式backgroundColor为vue列表

问题描述 投票:0回答:2

我有一个问题是更改活动列表的背景颜色。如果有人知道并帮助我,我将非常感激。

由于我从数据函数中获取列表内容以在HTML标记中使用v-for,因此我很困惑如何制作活动列表并对其进行样式化。

vue.js
2个回答
0
投票
<template>
  <div class="nav" :class="{ openMenu: menuVisible }">
    <div class="mouseover nav-toggle" @click="sideBarToggle">
      <a @mouseover="mouseover" @mouseleave="mouseleave">
        <img :src="image_src" class="nav-image">
      </a>
    </div>

    <div id="nav-content">
      <ul class="nav-list">
        <li
          v-for="(item, index) in items"
          :key="item.list"

          class="nav-list-contents"
        >
          <a>
            <router-link tag="div" :to="item.url" :class="{ 'active': index === 0 }">
              <i :class="item.icon"/>
              {{ item.list }}
            </router-link>
          </a>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations } = createNamespacedHelpers("moduleMenubar");
import Cookies from "js-cookie";
const hamburgerMenu = Cookies.get("openAndClose");

export default {
  data: function() {
    return {
      image_src: "humburger1.png",
      items: [
        { icon: "fa fa-fw fa-building", list: "企業管理", url: "/company" },
        { icon: "fa fa-fw fa-user", list: "ユーザー管理", url: "/user" },
        { icon: "fa fa-fw fa-database", list: "マスター管理", url: "/master" },
        {
          icon: "fa fa-fw fa-wrench",
          list: "法対応メンテナンス",
          url: "/legal_maintenance"
        },
        { icon: "fa fa-fw fa-newspaper", list: "お知らせ管理", url: "/info" },
        {
          icon: "fa fa-fw fa-sitemap",
          list: "企業グルーピング",
          url: "/grouping"
        }
      ]
    };
  },
  computed: {
    ...mapState(["menuVisible"])
  },
  methods: {
    ...mapMutations(["sideBarToggle"]),
    mouseover: function() {
      if (!this.menuVisible) {
        this.image_src = "migi.png";
      } else {
        this.image_src = "hidari.png";
      }
    },
    mouseleave: function() {
      this.image_src = "humburger1.png";
    }
  }
};
</script>

<style lang="scss" scoped>
@import "./public/common";

.active {
  background-color: red;
  width: 200px;
  height: 50px;
}

.fa {
  margin-right: 10px;
}

.nav-image {
  width: 15px;
  height: 15px;
}

.nav {
  font-size: 15px;
  width: 40px;
  margin: left;
  color: white;
  height: 100%;
  padding-top: 10px;
  background-color: #ab2a3e;
  position: relative;
  transition: width 0.3s ease 0;
  cursor: pointer;
  /*はみ出した文字を省略する*/
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.nav::-webkit-scrollbar {
  display: none;
}

.nav-list {
  padding: 10px; /* 左の余白削除 */
  margin-top: 50px;

  .nav-list-contents {
    cursor: pointer;

    list-style: none;
    width: 200px;
    height: 50px;
  }

  .nav-list-contents:hover {
    background-color: black;
  }
}

.nav.openMenu {
  width: 200px;
}

.nav-toggle {
  background-color: white;
  padding: 6px 6px;
  margin: 10px 5px;
  margin-bottom: 10px;
  float: right;
}
</style>

0
投票
<div v-for="item in list" :style="{backgroundColor: item.color, backgroundImage: 'url(' + item.imageUrl + ')'}"></div>
data () {
  return {
    list: [
      {color: '#ff0', imageUrl: 'https://.....'}
    ]
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.