简体   繁体   English

将鼠标悬停在卡片上时,我可以更改卡片的图像。 但是对于大量具有不同图像的卡片,我该怎么做

[英]I'm able to change the image of a card when it is being hovered over. But how do I do this for a large number of cards with different images

Here is an html sample for one card:这是一张卡的 html 样品:

        <div class="col s4">
          <div class="card">
            <div class="card-image">
              <img src="img/forZane/Bowl_Andrew (1).jpg" onmouseover="hover(this);" onmouseout="unhover(this);">
            </div>
            <div class="card-content">
              <h4 class="md-font">ceramic one.</h4>
              <p class="md-font">$50.</p>
            </div> 
          </div>
        </div>

and this is the javascript that i'm using:这是我正在使用的 javascript:

  element.setAttribute('src', 'img/forZane/Bowl_Andrew (2).jpg');
}

function unhover(element) {
  element.setAttribute('src', 'img/forZane/Bowl_Andrew (1).jpg');
}

I can't figure out how to apply this function/hover technique to different cards with different images.我不知道如何将此功能/悬停技术应用于具有不同图像的不同卡片。

Any help would be greatly appreciated.任何帮助将不胜感激。

You can do this for multiple images by introducing a new parameter to your functions that set the image url to that value:您可以通过向您的函数引入一个新参数来为多个图像执行此操作,将图像 url 设置为该值:

 function hover(element, url) element.setAttribute('src', url); } function unhover(element, url) { element.setAttribute('src', url); }
 <div class="col s4"> <div class="card"> <div class="card-image"> <img src="img/forZane/Bowl_Andrew (1).jpg" onmouseover="hover(this, 'img/forZane/Bowl_Andrew (2).jpg');" onmouseout="unhover(this, 'img/forZane/Bowl_Andrew (1).jpg');"> </div> <div class="card-content"> <h4 class="md-font">ceramic one.</h4> <p class="md-font">$50.</p> </div> </div> <div class="card"> <div class="card-image"> <img src="img/AnotherImage.jpg" onmouseover="hover(this, 'img/AnotherImageHover.jpg');" onmouseout="unhover(this, 'img/AnotherImage.jpg');"> </div> <div class="card-content"> <h4 class="md-font">wodden one.</h4> <p class="md-font">$70.</p> </div> </div> </div>

That way, you can have the same effect among multiple images.这样,您可以在多个图像之间产生相同的效果。

You can add custom data-* attributes to your img tags, so that your hover / unhover functions can change the image in each case.您可以将自定义data-*属性添加到您的img标签,以便您的hover / unhover函数可以在每种情况下更改图像。

Take this for example:以此为例:

 function hover(element) { element.setAttribute('src', element.getAttribute('data-hover')); } function unhover(element) { element.setAttribute('src', element.getAttribute('data-unhover')); }
 <div class="col s4"> <div class="card"> <div class="card-image"> <img src="img (1).jpg" data-hover="img (2).jpg" data-unhover="img (1).jpg" onmouseover="hover(this);" onmouseout="unhover(this);"> </div> <div class="card-content"> <h4 class="md-font">ceramic one.</h4> <p class="md-font">$50.</p> </div> </div> </div> <div class="col s4"> <div class="card"> <div class="card-image"> <img src="img (1).jpg" data-hover="img (2).jpg" data-unhover="img (1).jpg" onmouseover="hover(this);" onmouseout="unhover(this);"> </div> <div class="card-content"> <h4 class="md-font">ceramic one.</h4> <p class="md-font">$50.</p> </div> </div> </div>

If your filenames are indeed in a consistent format, and all you need to do is switch between filenames ending in "(1)" and "(2)", consider the following alternative.如果您的文件名确实具有一致的格式,并且您需要做的就是在以“(1)”和“(2)”结尾的文件名之间切换,请考虑以下替代方案。

Here, we remove the data-* attributes, and instead do a replacement of the src attribute.在这里,我们删除了data-*属性,而是替换了src属性。

 function hover(element) { element.setAttribute('src', element.getAttribute('src').replace('(1)', '(2)')); } function unhover(element) { element.setAttribute('src', element.getAttribute('src').replace('(2)', '(1)')); }
 <div class="col s4"> <div class="card"> <div class="card-image"> <img src="img (1).jpg" onmouseover="hover(this);" onmouseout="unhover(this);"> </div> <div class="card-content"> <h4 class="md-font">ceramic one.</h4> <p class="md-font">$50.</p> </div> </div> </div> <div class="col s4"> <div class="card"> <div class="card-image"> <img src="img (1).jpg" onmouseover="hover(this);" onmouseout="unhover(this);"> </div> <div class="card-content"> <h4 class="md-font">ceramic one.</h4> <p class="md-font">$50.</p> </div> </div> </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当其他元素悬停在元素上时,如何更改:hover状态? - How do I change :hover status on an element when a different element is hovered over? 如何在当前未悬停的所有其他图像上显示图像叠加? - How can I display an image overlay on all other images that are not currently being hovered over? 如何更改悬停项目的颜色? - How do I change the color of a hovered item? 将鼠标悬停在此网站上的“购物车”链接上时,如何实现显示的弹出框 - How do I implement this pop up box that displays when hovered over “Shopping Cart” link in this website 当有多张卡片从数据库中获取数据时,如何将卡片绑定到 Angular 中的按钮 - How do I bind a card to a button in Angular when there are multiple cards fetching data from database div悬停后,如何在div中更改文本? - How do I change the text inside of a div once the div is hovered? 当变体的色样悬停在 Shopify 上时,如何显示变体图像? - How do I display the variant image when the color swatch of a variant is hovered on Shopify? 如何使用鼠标悬停(或悬停)以便在 go 在不同的文本上显示不同的图像? JAVASCRIPT - How do I use mouseover (or hover) so that I can display different images when I go over different texts? JAVASCRIPT 提交时如何更改文本 - How do i change the text when i'm submitting 仅当元素在AngularJS中悬停时才如何分配变量? - How do I assign a variable only when an element is hovered in AngularJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM