Vue JS - 如何更改鼠标悬停时显示的组件的位置

Posted

技术标签:

【中文标题】Vue JS - 如何更改鼠标悬停时显示的组件的位置【英文标题】:Vue JS - How to change the position of a component that is displayed on mouse hover 【发布时间】:2021-07-25 06:34:50 【问题描述】:

我有一个与组件位置有关的问题。我有四张图片,当你将鼠标悬停在它们上面时,会显示某个组件,它看起来像这样

例如,如果将鼠标光标悬停在黄色图片上,则下方会显示另一个组件,以此类推所有组件

我的问题是当屏幕达到568像素时,即移动版,因为我需要改变显示组件的位置,因为此刻在移动版中我应该显示的组件看起来像这样

注意所有组件都显示在底部,我需要它们显示在我的每张图片的底部

也就是说,我希望它们看起来像这样。

可以看到给定的code in codesandbox

App.vue

<template>
  <div>
    <div class="enjoy_headline_container">
      <div class="EnjoyGirlsContainer">
        <div>
          <h3>Shrink the screen to 568 pixels or lower to see the problem</h3>
        </div>

        <div class="EnjoyGirlsList">
          <div
            v-for="(chunk, index) in Math.ceil(EnjoyGirlsList.length / 2)"
            :key="'chunk-' + index"
            :class="'wrap-' + index"
          >
            <div
              v-for="(item, index) in EnjoyGirlsList.slice(
                (chunk - 1) * 2,
                chunk * 2
              )"
              :key="'img-' + index"
              class="EnjoyCard"
              :class="'EnjoyCard-' + index"
            >
              <div>
                <img
                  @mouseover="mouseOver(item, (hover = true))"
                  v-bind:src="item.imagePath"
                  
                />
              </div>

              <div class="EnjoyCardContainer">
                <div
                  :style=" background: item.textColor "
                  class="EnjoyCardChildContainer"
                >
                  <h3 class="EnjoyCardChildContainerTitleName">
                     item.titleName 
                  </h3>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="EnjoyGirlsHoverEffect">
        <div
          class="HoverLogic"
          @mouseleave="mouseout(enjoy, (hover = false))"
          v-for="(enjoy, index) in EnjoyGirlsList"
          :key="index"
        >
          <div class="EnjoyGirlsChildHoverEffect">
            <component
              v-show="enjoy.hovered"
              v-bind:is="enjoy.componentName"
            ></component>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import EnjoyBlue from "./components/EnjoyBlue";
import EnjoyGreen from "./components/EnjoyGreen";
import EnjoyYellow from "./components/EnjoyYellow";
import EnjoyRed from "./components/EnjoyRed";

export default 
  name: "HomePage",
  components: 
    EnjoyRed,
    EnjoyYellow,
    EnjoyGreen,
    EnjoyBlue,
  ,

  data() 
    return 
      homePageImageList: [
        
          imageURL:
            "http://astragem.com/static/images/MenuGirl/HomePageBackground/15-min.png",
        ,
        
          imageURL:
            "http://astragem.com/static/images/MenuGirl/HomePageBackground/15-min.png",
        ,
        
          imageURL:
            "http://astragem.com/static/images/MenuGirl/HomePageBackground/15-min.png",
        ,
      ],
      hover: false,
      sectionGirlsListComponentsNames: [
        "EnjoyRed",
        "EnjoyYellow",
        "EnjoyGreen",
        "EnjoyBlue",
      ],
      EnjoyGirlsList: [
        
          imagePath:
            "https://lh3.googleusercontent.com/_0OiZeWgElIETUMZW8B9wEZR-V0BLMyDBHfK6hdYQVGzsryLQAZ0GEL9_PDi5NlzmpK8bETuJcZ0CtUQKnErvs36Xw=w640-h400-e365-rj-sc0x00ffffff",
          titleName: "TEENS",
          textColor: "#74C8C5",
          hovered: false,
          componentName: "EnjoyBlue",
        ,
        
          imagePath:
            "https://p0.piqsels.com/preview/32/831/578/leaf-malina-garden-nature-thumbnail.jpg",
          titleName: "MINXES",
          textColor: "#76ED00",
          hovered: false,
          componentName: "EnjoyGreen",
        ,
        
          imagePath:
            "https://dandelionmarketing.com/wp-content/uploads/2020/01/yellow-09.jpg",
          titleName: "MILFS",
          textColor: "#FFE600",
          hovered: false,
          componentName: "EnjoyYellow",
        ,
        
          imagePath:
            "http://pm1.narvii.com/6691/30c6c5246b1aee0e676f741f63ab144bbdb77da2_00.jpg",
          titleName: "COURGARS",
          textColor: "#CC003D",
          hovered: false,
          componentName: "EnjoyRed",
        ,
      ],

    ;
  ,
  methods: 

    mouseOver: function (enjoy) 
      this.EnjoyGirlsList.forEach((enjoy) => (enjoy.hovered = false));
      enjoy.hovered = true;
    ,

    mouseout: function (enjoy) 
      enjoy.hovered = false;
    ,

  ,
;
</script>

享受蓝色

<template>
   <p>Blue Component</p>
</template>

享受绿色

<template>
   <p>Green Component</p>
</template>

享受黄色

<template>
   <p>Yellow Component</p>
</template>

享受红色

<template>
   <p>Red Component</p>
</template>

【问题讨论】:

【参考方案1】:

有上百万种方法可以做到这一点。实现您想要的一种简单方法是将信息同时包含在“两个”位置,并使用 CSS 媒体查询控制其外观。见这里:https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries

所以基本上它看起来像这样

<Cougars>
 <EnjoyRed class="next-to-description" />
</Cougars>
<MINXES>
 <EnjoyGreen class="next-to-description" />
</MINXES>
<MILFS>
 <EnjoyYellow class="next-to-description" />
</MILFS>

<component :is="enjoy.componentName" class="below-all-description" />

然后设置一些css,例如:

.next-to-description 
  display: block;


.below-all-description 
  display: none;


@media screen and (min-width: 568px) 
  .next-to-description 
    display: none;
  
  .below-all-description 
    display: block;
  

现在您只需要确保每当&lt;Cougars&gt; 获得mouseover 事件时,您使用v-if 或其他内容填充EnjoyRed 组件,并继续使用&lt;component :is=".."&gt;

CSS 将控制并确保一次只显示 1 个。

我在 jsfiddle 上添加了我的想法的准系统示例:

https://jsfiddle.net/y8k1pgd9/

【讨论】:

非常感谢您关注我的问题,您无法在代码框中显示此示例,我在问题上留下了链接 我添加了一个 js fiddle 来简化代码,这样您就可以专注于必要的更改。另请注意,我的媒体查询中有一个错字 - 它应该是 min-width 非常感谢您一直以来的支持。

以上是关于Vue JS - 如何更改鼠标悬停时显示的组件的位置的主要内容,如果未能解决你的问题,请参考以下文章

display属性 鼠标悬停时显示隐藏内容,

Ext JS 网格面板:如何在鼠标悬停时显示内容

如何仅在悬停时显示截断的文本而不更改框列表的高度?

如何在 vue js 中的 AG Grid 自定义标题中添加图标

Twitter Bootstrap 选项卡:在悬停时显示,在鼠标输出时隐藏

在 reactjs 中悬停时显示组件 [关闭]