我有按钮类型的div,当您将鼠标悬停在它们上面时,会发生滑动效果。由于某种原因,每当我单击其中一个按钮并且活动班级就位时,悬停将不再起作用。

有什么办法可以使当前的活动设置有效?



$('.service-tab-block').click(function() {
    $('.service-tab-block').css({"background":"purple" , "color" : "#000"});
    $(this).css({"background":"#000", "color":"#FFF"});
  });

#service-tabs {
  width: 100%;
  padding: 100px 0;
  margin: 150px 0;
  height: auto;
}
#service-tabs-container {
  width: 100%;
  margin: 0 auto;
  text-align: center;
}
#service-tabs-left {
	display: inline-block;
	margin-right: 50px;
}
#service-tabs-right {
	display: inline-block;
	margin-left: 50px;
}

.service-tab-block {
	position: relative;
  font-size: 1.6em;
  padding: 1em 25px;
  text-align: center;
  display: block;
  margin: 30px 15px;
  cursor: pointer;
  /*background: purple;*/
    border: 1px solid purple;
  background-image: linear-gradient(to right, #000 50%, purple 50%);
  background-size: 200% 100%;
  background-repeat: no-repeat;
  background-position: bottom right;
  transition:width 0.3s ease;
  -webkit-transition:width 0.3s ease;
}

.service-tab-block:hover {
	-webkit-transition: all 0.3s ease-in;
  transition: all 0.3s ease-in;
   background-position: bottom left;
  color: #FFF;
  border: 1px solid #000;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="service-tabs">
	  <div id="service-tabs-container">
		<div id="service-tabs-left">
			<div class="service-tab-block" id="service_tab1">1</div>
			<div class="service-tab-block" id="service_tab2">2</div>
			<div class="service-tab-block" id="service_tab3">3</div>
		</div>
		<div id="service-tabs-right">
			<div class="service-tab-block" id="service_tab4">4</div>
			<div class="service-tab-block" id="service_tab5">5</div>
			<div class="service-tab-block" id="service_tab6">6</div>
		</div>
	  </div>
	</div>

最佳答案

为所有service-tab-blocks设置内联样式将覆盖您的样式表。尝试使用简单的类切换,如下所示。



$('.service-tab-block').click(function() {
    $('.service-tab-block').removeClass('active');
    $(this).toggleClass('active');
  });

#service-tabs {
  width: 100%;
  padding: 100px 0;
  margin: 150px 0;
  height: auto;
}
#service-tabs-container {
  width: 100%;
  margin: 0 auto;
  text-align: center;
}
#service-tabs-left {
	display: inline-block;
	margin-right: 50px;
}
#service-tabs-right {
	display: inline-block;
	margin-left: 50px;
}

.service-tab-block {
	position: relative;
  font-size: 1.6em;
  padding: 1em 25px;
  text-align: center;
  display: block;
  margin: 30px 15px;
  cursor: pointer;
  /*background: purple;*/
    border: 1px solid purple;
  background-image: linear-gradient(to right, #000 50%, purple 50%);
  background-size: 200% 100%;
  background-repeat: no-repeat;
  background-position: bottom right;
  transition:width 0.3s ease;
  -webkit-transition:width 0.3s ease;
}

.service-tab-block.active {
  background: black;
  color: white;
}

.service-tab-block:hover {
	-webkit-transition: all 0.3s ease-in;
  transition: all 0.3s ease-in;
   background-position: bottom left;
  color: #FFF;
  border: 1px solid #000;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="service-tabs">
	  <div id="service-tabs-container">
		<div id="service-tabs-left">
			<div class="service-tab-block" id="service_tab1">1</div>
			<div class="service-tab-block" id="service_tab2">2</div>
			<div class="service-tab-block" id="service_tab3">3</div>
		</div>
		<div id="service-tabs-right">
			<div class="service-tab-block" id="service_tab4">4</div>
			<div class="service-tab-block" id="service_tab5">5</div>
			<div class="service-tab-block" id="service_tab6">6</div>
		</div>
	  </div>
	</div>

关于javascript - 悬停和JavaScript事件问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37331932/

10-15 15:43