怎么设置鼠标移入移除时显示电影描述

问题遇到的现象和发生背景

怎么设置鼠标移入移除时显示电影描述,我想要的结果是鼠标放在某个图片上就会显示这个图片的提示,它是从上面移下来的,并且只显示这个图片的描述,其它图片不显示,移出后就往上滑,直至消失,这块我知道使用chuanslateY实现,我现在面临的难点就是怎么设置只显示这一个图片的描述,而不影响其它图片,我能想到的方法就是单独将图片和div描述挨个写出来,然后单独设置样式,但是这样的话太麻烦,想知道有什么好点的解决方法,

代码

<template>
  <div id="kuang_jia">
    <input id="chaxun" placeholder="请输入要查询的内容" type="text">
    <div id="img">
      <div ref="divImage" @mouseenter="mouseEnter" @mouseleave="mouseLeave">
        <img v-for="(item, index) in imgList" :key="index" alt="" v-bind:src="item" @mouseenter="imgEnter"
             @mouseleave="imgLeave"/>
      </div>
      <div v-for="(item, index) in miaoshuList" :key="index" ref="miaoshu" class="table{{ index }}">
        <div class="desc">{{ item.description }}</div>
      </div>
    </div>
    <span class="zuo" @click="zuo"> &lt; </span>
    <span class="you" @click="you"> &gt; </span>
  </div>
</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue'

export default {
  setup () {
    const imgList = ref([])
    const miaoshuList = ref([])

    onMounted(() => {
      imgList.value = [
        { src: 'one.jpg', alt: 'One' },
        { src: 'two.jpg', alt: 'Two' },
        { src: 'three.jpg', alt: 'Three' }
      ]
      miaoshuList.value = [
        { description: 'This is One' },
        { description: 'This is Two' },
        { description: 'This is Three' }
      ]
    })

    const mouseEnter = (index) => {
      miaoshuList.value[index].description = ''
      setTimeout(() => {
        miaoshuList.value[index].description = 'This is ' + imgList.value[index].alt
      }, 1000)
    }

    const mouseLeave = (index) => {
      miaoshuList.value[index].description = ''
    }

    return {
      imgList,
      miaoshuList,
      mouseEnter,
      mouseLeave
    }
  }
}
</script>

<style scoped>
.table {
  display: none;
}

.table:target {
  display: block;
}
</style>


想要实现的结果

我的代码中包含其它功能,你们回答的时候不用带我的原代码,只要实现我想要的功能就行,说白了就是你们写个相同的功能,我看下你们怎么写的,然后我进行借鉴,不要给我一堆我的代码,结果你们没改多少,这样我看着费劲,你们写着也费劲

  <div id="kuang_jia">
    <input id="chaxun" placeholder="请输入要查询的内容" type="text">
    <div id="img">
      <div ref="divImage" @mouseenter="mouseEenter" @mouseleave="mouseLeave">
        <div>
          <img v-for="(item, index) in imgList.array" :key="index" alt="" v-bind:src="item" @mouseenter="showDescription(index)"
               @mouseleave="hideDescription"/>
        </div>
        <table v-for="(item, index) in miaoshuList" id="miaoshu" :key="index"
               ref="miaoshu" v-bind:class="['table', index]" v-show="currentShow === index">
          <tr>
            <td>名称:{{ item.mingcheng }}</td>
          </tr>
          <tr>
            <td>时长:{{ item.shichang }}</td>
          </tr>
          <tr>
            <td>类型:{{ item.leixing }}</td>
          </tr>
          <tr>
            <td>导演:{{ item.daoyan }}</td>
          </tr>
        </table>
      </div>
    </div>
    <span class="zuo" @click="zuo"> &lt; </span>
    <span class="you" @click="you"> &gt; </span>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref, watch } from 'vue'
import one from '../../image/0a96e2a01593e9c8ce9ff190b1957172.jpg'
import tow from '../../image/0b2f6c5e2a7ea37e1f6cc2dd2d38f2f1.jpg'
import three from '../../image/1ac1c7bf66c43e0cd3ca51699c5206ba.jpeg'
import four from '../../image/5fec7cd428d3edd59870cf23c7c4149f.jpeg'
import five from '../../image/33dcfaedf645b04a7ded7e3a64b897ff.jpg'
import six from '../../image/9477156de6d94c49c505a9dfce8e8c17.jpg'
import seven from '../../image/c52be335098658931847fa0ecbcf7d03.jpeg'
import eight from '../../image/d86d80e8573ab91914a6e9ad5217c785.jpg'
import nine from '../../image/e305fca1a381bda8482d8b3296da61f6.jpg'
import ten from '../../image/fa983053c19cf3c7b27491507799e02e.jpeg'

const imgList = reactive({ array: [] })
imgList.array = [one, tow, three, four, five, six, seven, eight, nine, ten]
const divImage = ref()
const countChu = ref()
const width = ref()
let xunHuan1
const miaoshuList = ref()
let currentShow = ref(-1)

watch(countChu, () => {
  if (countChu.value > 5) {
    clearTimeout(xunHuan1)
  } else {
    xunHuan1 = setTimeout(() => {
      width.value = (-10) * countChu.value
      divImage.value.style.transform = 'translateX(' + width.value + '%)'
      divImage.value.style.transitionDuration = '1s'
      countChu.value++
    }, 3000)
  }
})

const zuo = () => {
  clearTimeout(xunHuan1)
  if (countChu.value <= 1) {
    return
  }
  width.value += 10
  countChu.value--
  divImage.value.style.transform = 'translateX(' + width.value + '%)'
  setTimeout(xunHuan1)
}

const you = () => {
  clearTimeout(xunHuan1)
  if (countChu.value >= 5) {
    return
  }
  width.value -= 10
  countChu.value++
  divImage.value.style.transform = 'translateX(' + width.value + '%)'
  setTimeout(xunHuan1)
}

onMounted(() => {
  countChu.value = 1
  miaoshuList.value = [
    { mingcheng: '凤皇传', shichang: '120分钟', leixing: '喜剧 / 古装', daoyan: '梁文弋' },
    { mingcheng: '风暴', shichang: '104分钟', leixing: '动作 犯罪 警匪', daoyan: '袁锦麟' },
    { mingcheng: '废柴特工', shichang: '96分钟', leixing: '喜剧 / 动作', daoyan: '玛·诺里扎德' },
    { mingcheng: '罪之声 ', shichang: '142分钟', leixing: '悬疑 / 犯罪', daoyan: '土井裕泰' },
    { mingcheng: '小妇人', shichang: '135分钟', leixing: '剧情 / 爱情', daoyan: '格蕾塔·葛韦格' },
    { mingcheng: '无人区', shichang: '117分钟', leixing: '剧情 / 犯罪 / 西部', daoyan: '宁浩' },
    { mingcheng: '妈妈!', shichang: '109分钟', leixing: '剧情', daoyan: '杨荔钠' },
    { mingcheng: '线人', shichang: '113分钟', leixing: '剧情 / 动作 / 惊悚', daoyan: '林超贤' },
    { mingcheng: '狙击手', shichang: '96分钟', leixing: '剧情 / 历史 / 战争', daoyan: '张艺谋 / 张末' },
    { mingcheng: '绝地逃亡', shichang: '111分钟', leixing: '喜剧 / 动作', daoyan: '雷尼·哈林' }
  ]
})

const mouseEenter = () => {
  clearTimeout(xunHuan1)
}

const mouseLeave = () => {
  setTimeout(xunHuan1)
}

const showDescription = (index) => {
  currentShow.value = index
}

const hideDescription = () => {
  currentShow.value = -1
}
</script>

<style scoped>
/* 你的CSS代码未变,保持原样。 */
</style>



采用chatgpt:
要实现鼠标移入移除时显示电影描述的功能,可以使用Vue的动态绑定和条件渲染来实现,无需为每个图片单独设置样式。

首先,为每个图片添加一个描述属性,将描述信息与每个图片关联起来:

const imgList = reactive({ 
  array: [
    { src: one, description: '凤皇传\n时长:120分钟\n类型:喜剧 / 古装\n导演:梁文弋' },
    { src: tow, description: '风暴\n时长:104分钟\n类型:动作 犯罪 警匪\n导演:袁锦麟' },
    // 其他图片的描述信息依次添加
  ]
})

然后,在模板中使用v-for遍历imgList.array,为每个图片绑定@mouseenter和@mouseleave事件,同时使用v-if指令来实现条件渲染:

<template>
  <!-- ... -->
  <div id="img">
    <div ref="divImage">
      <div v-for="(item, index) in imgList.array" :key="index">
        <img :src="item.src" @mouseenter="showDescription(item.description)" @mouseleave="hideDescription" />
        <div v-if="showDesc && currentDesc === item.description" id="miaoshu">{{ item.description }}</div>
      </div>
    </div>
  </div>
  <!-- ... -->
</template>

  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/7666213
  • 以下回答由chatgpt基于相关博客总结生成:

    首先,要实现鼠标移入移除时显示电影描述的效果,可以使用以下步骤:

    1. 在HTML中添加图片和对应的描述的标签,可以使用<img><p>标签,例如:

    ```html

    Movie 1

    Movie 1 Description

    Movie 2

    Movie 2 Description

    Movie 3

    Movie 3 Description

    ```

    1. 在CSS中设置描述标签的样式,让其初始时处于隐藏状态,例如:

    css .description { display: none; }

    1. 使用JavaScript为每个电影元素添加事件监听器,在鼠标移入时显示对应的描述,鼠标移出时隐藏描述。可以使用mouseentermouseleave事件,例如:

    ```javascript const movies = document.querySelectorAll('.movie'); // 获取所有电影元素

    movies.forEach(function(movie) { // 遍历每个电影元素 const description = movie.querySelector('.description'); // 获取当前电影元素下的描述元素 movie.addEventListener('mouseenter', function() { // 鼠标移入时显示描述 description.style.display = 'block'; }); movie.addEventListener('mouseleave', function() { // 鼠标移出时隐藏描述 description.style.display = 'none'; }); }); ```

    这样,当鼠标移入某个图片时,对应的描述会从上方滑动进入并只显示该图片的描述,其他图片的描述不会显示;当鼠标移出时,描述会向上滑动直至消失。

这个问题应该比较简单,主要是思路,思路对了代码好写。就是你要设定好鼠标移动进去某个图片后的动作,这个逻辑关系要用if来判断,就是如果鼠标移动进去了,那么图片就显示什么信息。移除的条件判断是类似的。

每一次解答都是一次用心理解的过程,期望对你有所帮助。
参考结合AI智能库,如有帮助,恭请采纳。

提供一个使用Vue.js实现鼠标移入移出时显示图片描述的示例代码。
代码解析:在这个示例中,使用Vue.js的ref和v-for指令来实现图片和描述的循环。使用setTimeout函数来描述的显示添加一点延迟,以模拟鼠标移入时显示描述的效果。为了隐藏描述,将其初始化为空字符串,并在鼠标离开时再次设置空字符串。这个示例中,假设图片和描述是一一对应的。当鼠标移入一张图片时,对应的描述会显示,而当鼠标移出时,描述会隐藏。你可以根据自己的需求来调整代码。

<template>  
  <div id="kuang_jia">  
    <input id="chaxun" placeholder="请输入要查询的内容" type="text">  
    <div id="img">  
      <div ref="divImage" @mouseenter="mouseEnter" @mouseleave="mouseLeave">  
        <img v-for="(item, index) in imgList" :key="index" alt="" v-bind:src="item" @mouseenter="imgEnter" @mouseleave="imgLeave"/>  
      </div>  
      <div v-for="(item, index) in miaoshuList" :key="index" ref="miaoshu" class="table{{ index }}">  
        <div class="desc">{{ item.description }}</div>  
      </div>  
    </div>  
    <span class="zuo" @click="zuo"> &lt; </span>  
    <span class="you" @click="you"> &gt; </span>  
  </div>  
</template>  
  
<script>  
import { ref, onMounted, onUnmounted } from 'vue'  
  
export default {  
  setup() {  
    const imgList = ref([])  
    const miaoshuList = ref([])  
  
    // 加载图片  
    onMounted(() => {  
      imgList.value = [  
        { src: 'one.jpg', alt: 'One' },  
         { src: 'two.jpg', alt: 'Two' },  
         { src: 'three.jpg', alt: 'Three' }  
      ]  
      miaoshuList.value = [  
        { description: 'This is One' },  
         { description: 'This is Two' },  
         { description: 'This is Three' }  
      ]  
    })  
  
    // 鼠标进入图片时显示描述  
    const mouseEnter = (index) => {  
      miaoshuList.value[index].description = ''  
      setTimeout(() => {  
        miaoshuList.value[index].description = 'This is ' + imgList.value[index].alt  
      }, 1000)  
    }  
  
    // 鼠标离开图片时隐藏描述  
    const mouseLeave = (index) => {  
      miaoshuList.value[index].description = ''  
    }  
  
    // 其他功能...  
  
    return {  
      imgList,  
      miaoshuList,  
      mouseEnter,  
      mouseLeave,  
      // ...其他方法和变量  
    }  
  }  
}  
</script>  
  
<style scoped>  
.table {  
  display: none;  
}  
.table:target {  
  display: block;  
}  
</style>


在Vue组件中添加currentIndex属性来记录当前鼠标悬停的图片的索引,使用@mouseenter@mouseleave事件绑定更新currentIndex的值,悬停图片时显示描述,其他图片隐藏,使用CSS动画或过渡效果控制描述的显示和隐藏,实现上下滑动的效果。