Becky
Becky

Reputation: 2289

Hover and javascript active issue

I have button type divs that when you hover over them a slide over effect happens. For some reason whenever I click on one of the buttons and my active class is in place, the hover does not work any longer.

Is there a way I can make this work with my current active set up?

$('.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>

Upvotes: 0

Views: 52

Answers (2)

Chris
Chris

Reputation: 159

Setting inline styles for all service-tab-blocks overrides your stylesheet. Try using a simple class toggle like the following.

$('.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>

Upvotes: 1

castletheperson
castletheperson

Reputation: 33466

It's because

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

Sets the inline style attribute of all of the .service-tab-block elements, which overrides the :hover styling in your CSS.

To make your :hover CSS take precedence, add !important to the styles that need to override whatever is in the style attribute.

.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;
}

$('.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!important;
  background: #000!important;
  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>

Upvotes: 1

Related Questions