悬停和javascript活动问题

时间:2016-05-19 18:59:22

标签: javascript jquery html css css3

我有按钮类型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>

2 个答案:

答案 0 :(得分:1)

为所有服务标签块设置内联样式会覆盖样式表。尝试使用如下的简单类切换。

&#13;
&#13;
$('.service-tab-block').click(function() {
    $('.service-tab-block').removeClass('active');
    $(this).toggleClass('active');
  });
&#13;
#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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这是因为

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

设置所有style元素的内联.service-tab-block属性,这会覆盖CSS中的:hover样式。

要使:hover CSS优先,请将!important添加到需要覆盖style属性中的任何内容的样式。

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

&#13;
&#13;
$('.service-tab-block').click(function() {
  $('.service-tab-block').css({"background": "purple", "color": "#000"});
  $(this).css({"background": "#000", "color": "#FFF"});
});
&#13;
#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!important;
  background: #000!important;
  border: 1px solid #000;
}
&#13;
<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>
&#13;
&#13;
&#13;