javascript - change background color of component on hover - Stack Overflow

Is there a way I can change the background color of the whole panel when I hover over any part of it?

Is there a way I can change the background color of the whole panel when I hover over any part of it?

I am using a basic boostrap panel, using bootstrap-react:

<LinkWrapper url={url}>
    <Panel header="text" bsStyle="primary">
    <p>text.</p>
    </Panel>
</LinkWrapper>

Which generates the following markup:

<div class="panel panel-primary" >
    <div class="panel-heading" >text</div>
    <div class="panel-body">
        <p>text</p>
    </div>
</div>

My linkWrapper ponent:

var LinkWrapper = React.createClass({
    getDefaultProps: function() {
        return {
        }
    },

    render: function() {
        return (
            <a href={this.props.url}>{this.props.children}</a>
        )
    }
});

module.exports = LinkWrapper;

My CSS is:

a.highlight:hover{
    text-decoration:none;
    color:white;
}

.highlight .panel-body:hover, .highlight .panel-heading:hover {
    text-decoration:none;
    background-color:$primary-color;
    color:white;
    border-color:$primary-color;
}

.highlight .panel-body p:hover {
    text-decoration:none;
    color:white;
}

Is there a way I can change the background color of the whole panel when I hover over any part of it?

I am using a basic boostrap panel, using bootstrap-react:

<LinkWrapper url={url}>
    <Panel header="text" bsStyle="primary">
    <p>text.</p>
    </Panel>
</LinkWrapper>

Which generates the following markup:

<div class="panel panel-primary" >
    <div class="panel-heading" >text</div>
    <div class="panel-body">
        <p>text</p>
    </div>
</div>

My linkWrapper ponent:

var LinkWrapper = React.createClass({
    getDefaultProps: function() {
        return {
        }
    },

    render: function() {
        return (
            <a href={this.props.url}>{this.props.children}</a>
        )
    }
});

module.exports = LinkWrapper;

My CSS is:

a.highlight:hover{
    text-decoration:none;
    color:white;
}

.highlight .panel-body:hover, .highlight .panel-heading:hover {
    text-decoration:none;
    background-color:$primary-color;
    color:white;
    border-color:$primary-color;
}

.highlight .panel-body p:hover {
    text-decoration:none;
    color:white;
}
Share Improve this question edited May 17, 2018 at 8:24 Waleed Iqbal 1,3222 gold badges20 silver badges37 bronze badges asked Oct 15, 2015 at 13:48 BomberBomber 11k27 gold badges97 silver badges175 bronze badges 2
  • Why wouldn't you use css? – nanobar Commented Oct 15, 2015 at 14:13
  • Unsure how to change 3 elements from one rule – Bomber Commented Oct 15, 2015 at 14:19
Add a ment  | 

1 Answer 1

Reset to default 2

The most simple solution uses CSS

#my-panel:hover * {
    background: gold;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn./bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn./bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div id="my-panel" class="panel panel-primary" >
    <div id="panel-header" class="panel-heading" >text</div>
    <div id="panel-body" class="panel-body" >
        <p >text</p>
    </div>
</div>

The above code finds the panel we're looking for and attaches the pseudo-element :hover to it. It then finds all of its descendants with the universal selector, *.

You can also do it in jQuery. Listen for the mouseover and mouseout events as they represent when a mouse is hovering over and away from an element resepectively.

During the mouseover event, add a class for changing the background color. But because Bootstrap classes have styles already, you need to use the !important declaration. During the mouseout event, simply remove this class.

$(document).ready(function() {
    	$(".panel").on("mouseover", function() {
            $(this).children().addClass("gold");
        }).on("mouseout", function() {
            $(this).children().removeClass("gold");
        });
    });
.gold { background: gold !important };
<script src="http://maxcdn.bootstrapcdn./bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn./bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="my-panel" class="panel panel-primary" >
    <div id="panel-header" class="panel-heading" >text</div>
    <div id="panel-body" class="panel-body" >
        <p >text</p>
    </div>
</div>

The JavaScript solution is similar to the jQuery one. Create variables for finding the body and header of the panel. You can listen for events either through inline events (ie onclick) or using addEventListener.

To add or remove classes, use the classList.add and classList.remove methods respectively.

var panel = document.getElementById("my-panel");
var panelHeader = document.getElementById("panel-header");
var panelBody = document.getElementById("panel-body");

panel.addEventListener("mouseover", function() {
    panelHeader.classList.add("gold");
    panelBody.classList.add("gold");
});

panel.addEventListener("mouseout", function() {
    panelHeader.classList.remove("gold");
    panelBody.classList.remove("gold");
});
.gold { background: gold !important };
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn./bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn./bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>

<div id="my-panel" class="panel panel-primary" >
    <div id="panel-header" class="panel-heading" >text</div>
    <div id="panel-body" class="panel-body" >
        <p >text</p>
    </div>
</div>

Fiddles

CSS: http://jsfiddle/5v0osxce/

jQuery: http://jsfiddle/fehhemvo/

JavaScript: http://jsfiddle/t3mafrnr/1

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744645935a4585615.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信