单击时保持元素的悬停样式处于活动状态

时间:2016-01-15 14:42:36

标签: javascript jquery html css

我正在制作一个选择小组,我很难搞清楚它的一个方面。有九个框,我希望用户能够点击框,当点击悬停的格式保持存在,然后理想情况下某种复选标记或添加到框的边框。我完全不确定当我关闭鼠标时如何让盒子的悬停字体效果保持不变。

有谁知道我该怎么做?

#project-scope-container {
	margin-top: 70px;
	margin-left: 9%;
	width: 75%;
	height: 300px;
}
#project-scope-title {
	font-size: 1.2em;
	font-weight: bold;
	margin-bottom: 15px;
}
.project-option-boxes {
	display: inline-block;
	border: 1px solid #45ba95;
	padding: 20px 0px;
	margin: 12px 20px 12px 0px;
	width: 30%;
	text-align: center;
	font-size: 1.2em;
	color: #45ba95;
	cursor: pointer;
}
.project-option-boxes:hover {
	background-color: #45ba95;
	color: #FFF;
}
<div id="project-scope-container">
		<div id="project-scope-title">PROJECT SCOPE</div>
		<div class="project-option-boxes">BRANDING & IDENTITY</div>
		<div class="project-option-boxes">WEB DESIGN</div>
		<div class="project-option-boxes">RESPONSIVE/MOBILE</div>
		<div class="project-option-boxes">MARKETING ASSETS</div>
		<div class="project-option-boxes">HTML5 ANIMATION</div>
		<div class="project-option-boxes">SEO OPTIMIZATION</div>
		<div class="project-option-boxes">MONTHLY SUPPORT</div>
		<div class="project-option-boxes">WEB DEVELOPMENT</div>
		<div class="project-option-boxes">ECOMMERCE</div>
	</div>

3 个答案:

答案 0 :(得分:2)

在悬停时创建另一个包含相同css样式的类名,并将这些类添加到单击的元素中或使用toggleClass,如下例所示:

&#13;
&#13;
$('.project-option-boxes').click(function() {
  $(this).hide().toggleClass('box_focused').fadeIn('slow');
});
&#13;
#project-scope-container {
  margin-top: 70px;
  margin-left: 9%;
  width: 75%;
  height: 300px;
}
#project-scope-title {
  font-size: 1.2em;
  font-weight: bold;
  margin-bottom: 15px;
}
.project-option-boxes {
  display: inline-block;
  border: 1px solid #45ba95;
  padding: 20px 0px;
  margin: 12px 20px 12px 0px;
  width: 30%;
  text-align: center;
  font-size: 1.2em;
  color: #45ba95;
  cursor: pointer;
}
.project-option-boxes:hover {
  background-color: #45ba95;
  color: #FFF;
}
.box_focused {
  background-color: #45ba95;
  background-image : url("http://findicons.com/files/icons/2232/wireframe_mono/48/checkbox_checked.png");
  background-position: right top;
  background-repeat: no-repeat;
  color: #FFF;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="project-scope-container">
  <div id="project-scope-title">PROJECT SCOPE</div>
  <div class="project-option-boxes">BRANDING & IDENTITY</div>
  <div class="project-option-boxes">WEB DESIGN</div>
  <div class="project-option-boxes">RESPONSIVE/MOBILE</div>
  <div class="project-option-boxes">MARKETING ASSETS</div>
  <div class="project-option-boxes">HTML5 ANIMATION</div>
  <div class="project-option-boxes">SEO OPTIMIZATION</div>
  <div class="project-option-boxes">MONTHLY SUPPORT</div>
  <div class="project-option-boxes">WEB DEVELOPMENT</div>
  <div class="project-option-boxes">ECOMMERCE</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用JQuery并使用.hover.addClass()来使用以下示例:

&#13;
&#13;
$(".project-option-boxes").hover(function()
{
  $(this).addClass("active");
});
&#13;
#project-scope-container {
	margin-top: 70px;
	margin-left: 9%;
	width: 75%;
	height: 300px;
}
#project-scope-title {
	font-size: 1.2em;
	font-weight: bold;
	margin-bottom: 15px;
}
.project-option-boxes {
	display: inline-block;
	border: 1px solid #45ba95;
	padding: 20px 0px;
	margin: 12px 20px 12px 0px;
	width: 30%;
	text-align: center;
	font-size: 1.2em;
	color: #45ba95;
	cursor: pointer;
}
.project-option-boxes:hover {
	background-color: #45ba95;
	color: #FFF;
}

.active {
  background: #45ba95;
  color: white;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="project-scope-container">
		<div id="project-scope-title">PROJECT SCOPE</div>
		<div class="project-option-boxes">BRANDING & IDENTITY</div>
		<div class="project-option-boxes">WEB DESIGN</div>
		<div class="project-option-boxes">RESPONSIVE/MOBILE</div>
		<div class="project-option-boxes">MARKETING ASSETS</div>
		<div class="project-option-boxes">HTML5 ANIMATION</div>
		<div class="project-option-boxes">SEO OPTIMIZATION</div>
		<div class="project-option-boxes">MONTHLY SUPPORT</div>
		<div class="project-option-boxes">WEB DEVELOPMENT</div>
		<div class="project-option-boxes">ECOMMERCE</div>
	</div>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以创建一个与悬停相同样式的类,当单击一个框时,您可以将此类添加到框中。

.project-option-boxes.active {
  background-color: #45ba95;
  color: #FFF;
}

并将课程提供给方框,

$(document).on('click', 'project-option-boxes', function (e) {
  $(this).toggleClass('active');
}