简体   繁体   English

选择正确的元素进行动画处理

[英]Selecting right element to animate

I'm trying to do a simple hover animation using jQuery, but I'm lost somewhere in selecting the right tags. 我正在尝试使用jQuery做一个简单的悬停动画,但我在选择正确的标签时迷失了方向。 Wordpress is not making it easy. Wordpress并不容易。

jQuery(document).hover(function() {
    jQuery('img').animate({ opacity: '0.5'});
}, function() {
    jQuery('img').animate({ opacity: '1'});
});

This code works fine, but as you can imagine, it changes the opacity of every single image on the page. 此代码工作正常,但您可以想象,它会更改页面上每个图像的不透明度。 Here comes my problem: what I'm supposed to put there instead of 'document', to change opacity of only one image? 这是我的问题:我应该把它放在那里而不是'文件',改变一个图像的不透明度? I've tried everything by now :P. 我现在已经尝试了一切:P。 Here's my little website I'm working on: www.klosinski.net . 这是我正在研究的小网站: www.klosinski.net

Edit - New Answer To change every image on its own hover event, apply the event to all img tags, and then use this to select that element: 编辑 - 新答案要更改自己的悬停事件中的每个图像,请将事件应用于所有img标记,然后使用this来选择该元素:

jQuery("img").hover(function() {
    jQuery(this).animate({ opacity: '0.5'});
}, function() {
    jQuery(this).animate({ opacity: '1'});
});

The below answer contains some good info, so I'll keep it in here. 下面的答案包含一些很好的信息,所以我会把它保存在这里。

Semi-irrelevant Answer Below 下面的半无关答案

You are selecting every img tag. 您正在选择每个img标签。 You need to come up with a way to uniquely identify the elements you want to change. 您需要想出一种方法来唯一标识要更改的元素。 The most used and most supported method of doing this is using either the class or id of the element. 最常用和最受支持的方法是使用元素的类或id。 In example, HTML: 在示例中,HTML:

<img src="lol.png" class="fadeThese" />

JS: JS:

$("img.fadeThese").fadeOut();

The selector inside the $() works just like a css selector, with a few added features. $()内的选择器就像一个css选择器,有一些附加功能。 Some of these additional features are : 其中一些附加功能包括

  • Attribute selection: $("a[href=google.com]") . 属性选择: $("a[href=google.com]") This will select all a tags with href equal to google.com 这将选择href等于google.com的所有标签
  • jQuery filters: $("div:nth-child(3)") gets the fourth child of an element jQuery过滤器: $("div:nth-child(3)")获取元素的第四个子元素
  • Selection by value: $("input[value=yes]") 按值选择: $("input[value=yes]")

For reference 以供参考

just give: 只是给:

$("#idoftheimage")  

instead of 代替

jQuery('img')

&

<img id='idoftheimage'>

要选择页面上的特定元素,请使用“#”标记。因此,如果元素的id为“Image1”,则可以使用jQuery('#Image1').animate({ opacity: '0.5'});

If you're used to CSS it's very similar. 如果你习惯了CSS它非常相似。 So as such you could do something like: 所以你可以这样做:

jQuery(div img.myimagetochangeopacity).hover(function() {
jQuery('img').animate({ opacity: '0.5'});
}, function() {
     jQuery('div img.myimagetochangeopacity').animate({ opacity: '1'});
});

In this case only images, with a class of myimagetochangeopacity, that are childs of a DIV will get affected. 在这种情况下,只有具有一类myimagetochangeopacity的图像才会受到影响。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM