[英]Hover and Click -> change color, click again switch to original ccolor
I have six elements and they should change in colors when I hover into the three buttons at the bottom. 我有六个元素,当我将鼠标悬停在底部的三个按钮中时,它们应该更改颜色。 Not only when I hover onto them, but when I click on to the button, the elements should stay changed in color, and on next click, I want it to go back to black.
不仅当我将鼠标悬停在它们上时,而且当我单击该按钮时,这些元素的颜色都应保持不变,并且在下次单击时,我希望它变回黑色。
I succeeded that the elements change colors when hover into the buttons, but I have trouble making it stay changed when it is clicked. 我成功地将元素悬停在按钮上时会更改颜色,但是我无法使其在单击时保持不变。
And on top of everything, the elements change to white when hover into the elements it self(not by the buttons on the bottom). 并且在所有内容的顶部,将鼠标悬停在其自身的元素上时,元素会变为白色(而不是通过底部的按钮)。
Here is the link of what I tried: 这是我尝试过的链接:
https://jsfiddle.net/ge9bw5nm/4/ https://jsfiddle.net/ge9bw5nm/4/
$(document).ready(function() { $(".colony_button").hover( function() { //mouse over $(this).css('color', '#b8aa85'); $(".colony_element").css('fill', '#b8aa85'); }, function() { //mouse out $(".colony_element").css('fill', "#000000"); $(this).css('color', "#000000"); }); $(".prison_button").hover( function() { //mouse over $(this).css('color', '#3268bf'); $(".prison_element").css('fill', '#3268bf'); }, function() { //mouse out $(this).css('color', "#000000"); $(".prison_element").css('fill', "#000000"); }); $(".open_button").hover( function() { //mouse over $(this).css('color', '#e4cb3e'); $(".open_element").css('fill', '#e4cb3e'); }, function() { //mouse out $(this).css('color', "#000000"); $(".open_element").css('fill', "#000000"); }); });
.geomap { top: 0; width: 100%; cursor: grab; } .timeline { font-size: 2vw; position: fixed; bottom: 0; left: 0; background: rgba(204, 204, 204, 0.5); padding: 1em 2em 1em 2em; margin: 1em; border-radius: 1.4em; text-align: center; } .colonybutton, .prisonbutton, .openbutton { width: 1em; padding: 0.3em 4em 0 4em; margin: 0; } .colony_button:hover, .prison_button:hover, .open_button:hover { cursor: pointer; } .colony, .prison, .open { display: inline-block; /*margin-bottom: 1em;*/ padding: 0; } .colony_element, .open_element, .prison_element { transition: 0.8s; } .colony_element:hover, .open_element:hover, .prison_element:hover { fill: white ! important; transition: 0.8s; cursor: pointer; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <svg class="geomap" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1331.7 883" style="enable-background:new 0 0 1331.7 883;" xml:space="preserve"> <g id="colony_projects"> <circle class="colony_element" cx="618" cy="411.2" r="8"/> <circle class="colony_element" cx="666.8" cy="274.8" r="8"/> </g> <g id="prison_projects"> <circle class="prison_element" cx="461.2" cy="156.6" r="8"/> <circle class="prison_element" cx="456.9" cy="163.5" r="8"/> </g> <g id="open_projects"> <circle class="open_element" cx="522.8" cy="275.8" r="8"/> <circle class="open_element" cx="352.8" cy="451" r="8"/> </svg> <div class="timeline"> <div class="timetext"> <div class="colony"> <div class="colony_button">⬤</div> </div> <div class="prison"> <div class="prison_button">⬤</div> </div> <div class="open"> <div class="open_button">⬤</div> </div> </div> </div>
There are several modifications I made to your original code to make it work: 为了使它正常工作,我对您的原始代码进行了一些修改:
</g>
in the SVG markup </g>
active
is toggled active
的切换 .active
state of the dots .active
状态添加了CSS规则 !important
on the :hover
for colouring the dots in white :hover
上的!important
将白色的点着色 You might also want to change all the setting of color/fill via JS to adding and removing a class on hover/mouse out, so you can set the colours in a single place via CSS. 您可能还希望通过JS更改颜色/填充的所有设置,以在悬停/鼠标移出时添加和删除类,因此可以通过CSS在单个位置设置颜色。
$(document).ready(function() { $(".colony_button").hover( function() { //mouse over $(this).css('color', '#b8aa85'); $(".colony_element").css('fill', '#b8aa85'); }, function() { //mouse out $(".colony_element").css('fill', ""); $(this).css('color', ""); }).click(function() { $(".colony_element").toggleClass('active'); }); $(".prison_button").hover( function() { //mouse over $(this).css('color', '#3268bf'); $(".prison_element").css('fill', '#3268bf'); }, function() { //mouse out $(this).css('color', ""); $(".prison_element").css('fill', ""); }).click(function() { $(".prison_element").toggleClass('active'); }); $(".open_button").hover( function() { //mouse over $(this).css('color', '#e4cb3e'); $(".open_element").css('fill', '#e4cb3e'); }, function() { //mouse out $(this).css('color', ""); $(".open_element").css('fill', ""); }).click(function() { $(".open_element").toggleClass('active'); }); });
.geomap { top: 0; width: 100%; cursor: grab; } .timeline { font-size: 2vw; position: fixed; bottom: 0; left: 0; background: rgba(204, 204, 204, 0.5); padding: 1em 2em 1em 2em; margin: 1em; border-radius: 1.4em; text-align: center; } .colonybutton, .prisonbutton, .openbutton { width: 1em; padding: 0.3em 4em 0 4em; margin: 0; } .colony_button:hover, .prison_button:hover, .open_button:hover { cursor: pointer; } .colony, .prison, .open { display: inline-block; /*margin-bottom: 1em;*/ padding: 0; } .colony_element, .open_element, .prison_element { transition: 0.8s; } .colony_element.active { fill: #b8aa85; } .prison_element.active { fill: #3268bf; } .open_element.active { fill: #e4cb3e; } .colony_element:hover, .open_element:hover, .prison_element:hover { fill: white; transition: 0.8s; cursor: pointer; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <svg class="geomap" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1331.7 883" style="enable-background:new 0 0 1331.7 883;" xml:space="preserve"> <g id="colony_projects"> <circle class="colony_element" cx="618" cy="411.2" r="8"/> <circle class="colony_element" cx="666.8" cy="274.8" r="8"/> </g> <g id="prison_projects"> <circle class="prison_element" cx="461.2" cy="156.6" r="8"/> <circle class="prison_element" cx="456.9" cy="163.5" r="8"/> </g> <g id="open_projects"> <circle class="open_element" cx="522.8" cy="275.8" r="8"/> <circle class="open_element" cx="352.8" cy="451" r="8"/> </g> </svg> <div class="timeline"> <div class="timetext"> <div class="colony"> <div class="colony_button">⬤</div> </div> <div class="prison"> <div class="prison_button">⬤</div> </div> <div class="open"> <div class="open_button">⬤</div> </div> </div> </div>
There is too much to go through in the original code. 原始代码中要处理的内容太多。 So, I'll give you the simplified version of what you need.
因此,我将为您提供所需内容的简化版本。
https://jsfiddle.net/2hjm0tnb/ https://jsfiddle.net/2hjm0tnb/
In a nutshell, you want to create a new class and operate with it. 简而言之,您想创建一个新类并对其进行操作。
Let's say these are 3 elements you are working with. 假设这些是您正在使用的3个元素。 you want them to have the same class to shorten the code
您希望他们拥有相同的班级以缩短代码
<div class="testclass"></div>
<div class="testclass"></div>
<div class="testclass"></div>
In css, we use the original class for original styling and add another class for the change 在CSS中,我们将原始类用于原始样式,并添加另一个类进行更改
.testclass {
background-color:#ccc;
width:50px;
height:50px;
margin-top:10px;
}
.active {background-color:#000;}
// here we say that this will work for every element with original class
$(".testclass").each(function(){
//here we declare counter to count number of clicks
var counter = 1;
//add class with changed style on mouse over
$(this).mouseover(function(){
$(this).addClass('active');
});
// this will define actions on click
$(this).click(function(){
//add 1 to counter
counter++;
//if counter is odd, it will add the class. If not- will do nothing
if (counter % 2 == 1) {
$(this).toggleClass('active');
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.