这里的问题是,当我将鼠标悬停在特定的 div 上时,jquery 脚本将应用于具有相同类的每个 div。我希望该脚本仅适用于悬停元素,即使其他 div 具有相同的类也是如此。为了方便起见,我没有在其中添加 css ID。 这是我的 jQuery 代码:
jQuery(".enprod_grille .isotope-container").hover(function() {
// do this on hover
jQuery('.enprod_grille .post-thumb').animate({
'marginRight': '0px',
'opacity': '0.4'
}, 'fast');
jQuery('.enprod_grille .post-title').animate({
'paddingLeft': '0px',
'opacity': '1'
}, 'fast');
jQuery('.enprod_grille .entry-content').animate({
'paddingLeft': '0px',
'opacity': '1'
}, 'fast');
},function() {
// do this on hover out
jQuery('.enprod_grille .post-thumb').animate({
'marginRight': '45px',
'opacity': '1'
}, 'fast');
jQuery('.enprod_grille .post-title').animate({
'paddingLeft': '20px',
'opacity': '0'
}, 'fast');
jQuery('.enprod_grille .entry-content').animate({
'paddingLeft': '20px',
'opacity': '0'
}, 'fast');
});
这是网站的链接,它出现在右下角的“En production”栏中:http://mwww.fr/fullhouse/?lang=fr
最佳答案
使用 this
定位当前 div,例如:
jQuery(".enprod_grille .isotope-container").hover(function() {
// do this on hover
jQuery(this).find('.post-thumb').animate({
'marginRight': '0px',
'opacity': '0.4'
}, 'fast');
jQuery(this).find('.post-title').animate({
'paddingLeft': '0px',
'opacity': '1'
}, 'fast');
jQuery(this).find('.entry-content').animate({
'paddingLeft': '0px',
'opacity': '1'
}, 'fast');
}, function() {
// do this on hover out
jQuery(this).find('.post-thumb').animate({
'marginRight': '45px',
'opacity': '1'
}, 'fast');
jQuery(this).find('.post-title').animate({
'paddingLeft': '20px',
'opacity': '0'
}, 'fast');
jQuery(this).find('.entry-content').animate({
'paddingLeft': '20px',
'opacity': '0'
}, 'fast');
});
关于javascript - 当其他 div 具有相同的类时,如何要求 jquery 应用于一个 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30051928/