简体   繁体   English

悬停并单击->更改颜色,再次单击切换到原始ccolor

[英]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">&#11044;</div> </div> <div class="prison"> <div class="prison_button">&#11044;</div> </div> <div class="open"> <div class="open_button">&#11044;</div> </div> </div> </div> 

There are several modifications I made to your original code to make it work: 为了使它正常工作,我对您的原始代码进行了一些修改:

  • Fixed the missing closing </g> in the SVG markup 修复了SVG标记中缺少的结束</g>
  • Registered click listeners in addition to the hover where a class active is toggled 已注册的点击侦听器,以及将鼠标悬停在其中的类的active的切换
  • Added CSS rules for the .active state of the dots 为点的.active状态添加了CSS规则
  • Instead of setting the color to black explicitly on mouse out, just reset the css property by setting it to an empty string, therefore you don't need the !important on the :hover for colouring the dots in white 无需在鼠标移开时将颜色显式设置为黑色,只需将css属性设置为空字符串即可将其重置,因此,您不需要: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">&#11044;</div> </div> <div class="prison"> <div class="prison_button">&#11044;</div> </div> <div class="open"> <div class="open_button">&#11044;</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.

相关问题 更改悬停颜色按钮单击 - change hover color button click 我想更改一次按钮的背景色,一旦我单击另一个按钮就再次更改为原始颜色 - i want to change the background color of a button once clicked and change again to original color once i click another button 悬停/单击时更改内容 - Change content on hover/click 单击时更改 Chartjs 标签颜色而不会丢失悬停 - Change Chartjs label color on click without losing hover 传单地图单击更改颜色,然后再次单击时删除 - Leaflet map click change color and then remove when clicking again 我有一个按钮,我需要在第一次单击时更改颜色并在第二次单击时更改回原始颜色 - I have a button I need to turn color on fist click and change back to original color on second click HTML:单击按钮,它会改变颜色。 然后单击按钮,但是,它不会更改为原始颜色 - HTML: click the button, it changes color. Then click the button, however, it doesn't change to original color 尝试将汉堡按钮的背景颜色更改为其他颜色,然后再次单击将其更改为与取消单击相同的颜色 - Trying do change background color of the hamburger button to other color, and click again it will change it to back to same color as unclick 如何在单击时更改按钮的 state 并在 React 中再次单击时恢复为原始 state? - How to change state of button on click and revert back to the original state when clicked again in React? Boostrap在点击时改变颜色 - Boostrap change color on click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM