简体   繁体   English

在DOM更改时触发:hover重绘

[英]Triggering :hover Repaint on DOM change

I have UI control in a div. 我在div中有UI控件。 The entire div has a hover effect. 整个div都有悬停效果。 If the user gets an error, an absolutely positioned div covers the control and asks how to proceed. 如果用户遇到错误,则绝对定位的div会覆盖控件并询问如何继续。 The control is no longer hovered, but a repaint isn't triggered until the user moves the mouse. 控件不再悬停,但是直到用户移动鼠标后才触发重新绘制。 Is there a known way to solve this problem? 有解决此问题的已知方法吗? I'm using Chrome 我正在使用Chrome

Here's a Fiddle: http://jsfiddle.net/acbabis/L655r/ 这是一个小提琴: http : //jsfiddle.net/acbabis/L655r/

HTML: HTML:

<div class="container">
    <div class="hoverable inner-div">
        <button class="clickme">Click Me</button><br/>
    </div>
</div>

JS: JS:

$(function() {
    $('.clickme').one('click', function() {
        $('.hoverable').append(
            '<br/><div class="touch">Move the mouse here. Hold Still</div>');
    })

    $('.container').on('mouseenter', '.touch', function() {
        setTimeout(function() {
        $('.container').append(
            '<div class="cover inner-div"><br/><br/><br/>Move the mouse now</div>');
        }, 1000);
    });
});

CSS: CSS:

.container {
    width: 300px;
    height: 300px;
    margin: 0;
    padding: 0;
    position: relative;
}

.inner-div {
    width: 100%;
    height: 100%;
    padding: 50px 100px;
    text-align: center;
    box-sizing: border-box;
}

.hoverable {
    background-color: blue;
}

.hoverable:hover {
    background-color: green;
}

.cover {
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(150, 150, 150, .5);
    padding: 150px 100px 50px 100px;
}

EDIT: I'm aware there are ways to solve this with JS, and I'm willing to use JS; 编辑:我知道有使用JS解决此问题的方法,并且我愿意使用JS。 but I'm curious if anyone can show me a CSS/HTML-only way to trigger a repaint. 但我很好奇是否有人可以向我展示仅CSS / HTML的方式来触发重新绘制。 Of course, if I only get JS solutions, I'll accept the best one. 当然,如果我只得到JS解决方案,我会接受最好的解决方案。

The css way CSS方式

You could add a css rule based on a sibling selector like this: 您可以像这样添加基于兄弟选择器的css规则:

.cover + .hoverable  {
    background-color: blue;
 }

This assumes you prepend .cover to .container and you probably have to set z-indices on .cover and .hoverable . 这是假设你prepend .cover.container ,你可能必须设置在z折射率.cover.hoverable

Reflected in this fiddle: http://jsfiddle.net/L655r/8/ 反映在这个小提琴中: http : //jsfiddle.net/L655r/8/

The js way js方式

You could generally force a repaint on your .hoverable or the .clickme with this simple 通常,您可以使用以下简单方法在.hoverable.clickme上强制重绘

jQuery plugin : jQuery插件

$(function() {
    $.fn.redraw = function() {
        var $this = $(this);
        $this.hide();

        setTimeout(function(){
            $this.show();
        },0);

        return $this;
    };
});

Usage : 用法

$('.hoverable').redraw();

Fiddle : 小提琴

http://jsfiddle.net/marionebl/L655r/5/ http://jsfiddle.net/marionebl/L655r/5/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM