【问题标题】:When hover removeClass, addClass to the class the removed when mouseenter another dive当鼠标悬停removeClass时,将鼠标添加到另一个潜水时删除的类
【发布时间】:2016-11-17 23:48:25
【问题描述】:
<script>
$('.image-wrapper').mouseenter(function(e) {
  e.preventDefault();
  $(this).removeClass('image-wrapper');
});

$('.boxer').mouseenter(function(){
  $('.image_wrapper img').addClass('image_wrapper img:hover');
});
</script>

如你所见,我为我的背景 &lt;div&gt; 悬停效果删除了类。所以当我选择另一个&lt;div&gt; 时,只让背景&lt;div&gt; 悬停。

更新*我也试试这个。

<script>
var background = document.getElementById('.image-wrapper');
var background = false ;

$('.image-wrapper').mouseenter(function(e) {
    e.preventDefault();
    $(this).removeClass('image-wrapper');
    background = true;
 });

$('.boxer').mouseenter(function(){
    if (background = true) {
        $('.image-wrapper').addClass('image-wrapper');
    } else {
        $('.image-wrapper').removeClass('image-wrapper');
    }
});
</script>

HTML:

<div id="info_01" class="image-wrapper info_all">
    <img src="images/main_2/info_01.jpg" alt="" class="images">
    <div class="boxer"><p class="boxer_title">ABOUT</p></div>
</div>

CSS:

#info_01 {
    width:512px;
    height:384px;
    background:no-repeat;
    position:absolute;
}

.image-wrapper {
  font-size: 0;
  overflow:hidden;
}

.image-wrapper img {
  max-width: 100%;
  transition: transform .3s ease-in-out;
}

.image-wrapper:hover img {
  transform: scale(1.1); 
}

【问题讨论】:

  • 如果你删除了元素的.image-wrapper 类,第二个函数会自动找到元素。显示更多代码让我们了解您真正想要做什么。因为现在,你问的还不清楚,。
  • 我不知道该怎么说。但也许我可以向你解释。假设我有一个带有 w:50px h:50px 的 ,现在当我将其悬停时缩放到 1.1,然后我在 中有一个带有 w:10px h:10px 的 ,所以现在我做一个Jquery 删除 悬停效果。然后现在当我将鼠标移动到 时, 开始悬停。
  • 我可以知道 document.getElementById('.class');因为通过“ID”获取元素,但最后我使用 .class 可以吗?
  • document.getElementById() 是一种选择元素的 Javascript 方法。它绝对需要id。如果你想选择一个班级,请使用document.getElementsByClassName()
  • 但是我的意思是...如果您首先从元素中删除该类,则不能使用此类在第二个函数中选择元素...因为该类已被删除。展示您的 HTML 和 CSS(如果有)...也许我们会找到解决方案。

标签: jquery hover mouseenter


【解决方案1】:

使用您的 HTML 和 CSS,可以更轻松地理解您想要实现的目标。
您应该始终将其包含在您以后的问题中。

然后描述你想要的结果。
所以,这里...您希望图像下方的文本在图像放大的同时出现。

我把image-wrappercss规则的名字改成了font-zero...这样更能说明它的真实效果。

我使用的jQuery方法是.hover()
对于 mouseenter 和 mouseleave,有两个函数充当事件 handlers。

关于按类选择元素与添加和删除元素:
使用“稳定”选择器来定位元素。

您添加和删除的类也不应该用作选择器...
除非您想“阻止”某个函数触发...
如果选择器已被删除,不要期望函数会触发。
这是你迷失自我的地方。

您可以使用大量的 jQuery 方法来简化您的脚本。
我建议你收藏this link。

$('#info_01 img').hover(
	function(e) {
		$(this).parent().removeClass('font-zero');
	},
	function(){
		$(this).parent().addClass('font-zero');
});
#info_01 {
  width:512px;
  height:384px;
  background:no-repeat;
  position:absolute;
}

.font-zero {
  font-size: 0;
  overflow:hidden;
}

.image-wrapper img {
  max-width: 100%;
  transition: transform .3s ease-in-out;
}

.image-wrapper:hover img {
  transform: scale(1.1); 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="info_01" class="image-wrapper font-zero">
	<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRlOChoWeklUXYE3aE4hIbKIwDKheJoxvHWkcjAeSF2fXxDDtvZ" alt="" class="images">
	<div class="boxer"><p class="boxer_title">ABOUT</p></div>
</div>

【讨论】:

    猜你喜欢
    • 2015-03-29
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多