最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make hover effect stays even after unhover? - Stack Overflow

programmeradmin1浏览0评论

I have created a simple JSFiddle for the problem. Here is the link: /

CSS:

.container{
 background: white;
 display:flex;
 justify-content: center;
 align-items: center;
 height:50px
}

.circle {
 display: inline-block;
 width: 20px;
 height: 20px;
 background: #0f3757;
 -moz-border-radius: 50px;
 -webkit-border-radius: 50px;
 border-radius: 50px;
 margin-left:10px;
 float:left;
 transition: all 0.3s ease
}

.circle:hover {
 background:orange;
}

Basically over here, I can hover on any circle to change their color. I would like to ask how could I make the orange color stays on on any particular circle that I hovered on after the mouse moved away to white container?

Any script or CSS animation I could use to solve the problem?

I have created a simple JSFiddle for the problem. Here is the link: https://jsfiddle/tnkh/Loewjnr3/

CSS:

.container{
 background: white;
 display:flex;
 justify-content: center;
 align-items: center;
 height:50px
}

.circle {
 display: inline-block;
 width: 20px;
 height: 20px;
 background: #0f3757;
 -moz-border-radius: 50px;
 -webkit-border-radius: 50px;
 border-radius: 50px;
 margin-left:10px;
 float:left;
 transition: all 0.3s ease
}

.circle:hover {
 background:orange;
}

Basically over here, I can hover on any circle to change their color. I would like to ask how could I make the orange color stays on on any particular circle that I hovered on after the mouse moved away to white container?

Any script or CSS animation I could use to solve the problem?

Share Improve this question asked Jun 13, 2017 at 4:40 tnkhtnkh 1,8392 gold badges17 silver badges33 bronze badges 2
  • So, when you move out of white container, it should be removed? – Guruprasad J Rao Commented Jun 13, 2017 at 4:45
  • @GuruprasadRao Nope, the hover effect should stay on – tnkh Commented Jun 13, 2017 at 5:33
Add a ment  | 

5 Answers 5

Reset to default 4

Just add an mouseover event to .circle element and write an active CSS class which has background-color property and when event occurs remove active class from any .circle and add it to current element

JS

$(".container span.circle").on('mouseover',function(){
    $(".circle").removeClass('active');//remove from other elements
    $(this).addClass('active');
});

CSS

.active {
  background:orange;
  transition: all 0.5s ease
}

Updated Fiddle

Using JQuery you can add a class to an element as such:

$(element).on('hover', function() {
    // this if you're hovering over the element that would change, otherwise rename 'this' to whatever element class or id you want to change 
    $(this).addClass('NameOfClass');
});

You can then have that class in CSS

.NameOfClass {
    background-color: orange;
}

And then just remove that class when you want.

Change .circle:hover to .hover

.hover {
  background:orange;
  transition: all 0.5s ease
}

A) IF you want orange color be forever :

Use this jquery

$(document).ready(function(){
    $('.circle').hover(function(){
        $(this).addClass('hover');
    })
})

$(document).ready(function(){
    $('.circle').hover(function(){
        $(this).addClass('hover');
    })
})
.container{
    background: white;
    display:flex;
    justify-content: center;
    align-items: center;
    height:50px
}

.circle {
    display: inline-block;
    width: 20px;
    height: 20px;
    background: #0f3757;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 50px;
    margin-left:10px;
    float:left;
    transition: all 0.5s ease
}

.hover {
    background:orange;
    transition: all 0.5s ease
}
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class= "container">
  <span class="circle"></span>
  <span class="circle"></span>
  <span class="circle"></span>
  <span class="circle"></span>
</div> 

B) If you want orange color be until mouse move on other circle

Use this jquery

$(document).ready(function(){
    $('.circle').hover(function(){
        $('.circle').removeClass('hover');
        $(this).addClass('hover');
    })
})

$(document).ready(function(){
    $('.circle').hover(function(){
        $('.circle').removeClass('hover');
        $(this).addClass('hover');
    })
})
.container{
    background: white;
    display:flex;
    justify-content: center;
    align-items: center;
    height:50px
}

.circle {
    display: inline-block;
    width: 20px;
    height: 20px;
    background: #0f3757;
    -moz-border-radius: 50px;
    -webkit-border-radius: 50px;
    border-radius: 50px;
    margin-left:10px;
    float:left;
    transition: all 0.5s ease
}

.hover {
    background:orange;
    transition: all 0.5s ease
}
<script src="https://ajax.googleapis./ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class= "container">
  <span class="circle"></span>
  <span class="circle"></span>
  <span class="circle"></span>
  <span class="circle"></span>
</div> 

You can use Jquery to set a class when the mouse is hovered. Then the class will remain set even after the mouse moves away.

$(".circle").hover(function() {
    $(this).addClass("hovered");
});

I have created a jsfiddle to demonstrate.

$( ".circle" ).mouseover(function(){
    $(this).css('background','orange')
  }
)

https://jsfiddle/rtxq9fnu/

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>