简体   繁体   English

单击它后不能再悬停div

[英]Can't hover no more a div after I've clicked on it

I'm actually trying to do a menu using CSS and jQuery. 我实际上正在尝试使用CSS和jQuery进行菜单。 It uses images 2 images per button, one for when it's not active and the other for the hover. 每个按钮使用2张图片,一张用于不活动时,另一张用于悬停。 This part is made with the stylesheet. 这部分由样式表组成。 When you click on a li, it take the same style as if the mouse was over. 单击li时,其样式与鼠标悬停时相同。 But only one li can be clicked at a time so if you click on another(or the same) li it undo the previous one for the other(or none). 但是一次只能单击一个li,因此,如果单击另一个(或相同)li,它将撤销另一个(或没有)的前一个li。 This part is done with jquery. 这部分是通过jquery完成的。

My problem is that when I click on a LI and then unselect it, I can no longer hover it. 我的问题是,当我单击一个LI并取消选择它时,我将无法再将其悬停。 I think there is a collision between the CSS applied by jQuery and the style sheet. 我认为jQuery应用的CSS与样式表之间存在冲突。

There is the HTML Part : 有HTML部分:

<body>
<div id="wrap"><!--Header Menu TagBar -->
    <div id="header"></div>

    <div id="menu">
        <ul>
            <li id="genre"></li>

            <li id="author"></li>

            <li id="home">
                <div id="hmIcon"></div>
            </li>

            <li id="artist"></li>

            <li id="year"></li>
        </ul>
    </div>

This is the CSS Part : 这是CSS部分:

#menu {
background-color:#fff;
width:1000px;
height:60px;
position:relative;
}

#menu ul {
margin:0;
padding:0;
list-style:none;
overflow: hidden;
}

#menu ul li {
width:200px;
float:left;
list-style:none;
text-align:center;
height:60px;
line-height:60px;
opacity:.9;
}

#menu li:hover {
opacity:1;
box-shadow: inset 0px 0px 4px 2px rgba(0,0,0,0.6);
}

#genre {
background-image:url(../images/genreHover.jpg);
background-repeat:no-repeat;
background-position:center center;
background-size:200px 60px;
}

#genre:hover {
background-image:url(../images/genre.jpg);
}

#author {
background-image:url(../images/authorHover.jpg);
background-repeat:no-repeat;
background-position:center center;
background-size:200px 60px;
}

#author:hover {
background-image:url(../images/author.jpg);
}

#hmIcon {
width:100%;
height:100%;
background-image:url(../images/HomeIcon.png);
background-position:center center;
background-size:50px 50px;
background-repeat:no-repeat;
}

#home {
background-image:url(../images/homeHover.jpg);
background-repeat:no-repeat;
background-position:center center;
background-size:200px 60px;
}

#home:hover {
background-image:url(../images/home.jpg);
}

#artist  {
background-image:url(../images/artistHover.jpg);
background-repeat:no-repeat;
background-position:center center;
background-size:200px 60px;
}

#artist:hover {
background-image:url(../images/artist.jpg);
}

#year {
background-image:url(../images/yearContour.jpg);
background-repeat:no-repeat;
background-position:center center;
background-size:200px 60px;
}

#year:hover {
background-image:url(../images/year.jpg);
}

And the best for the last (the headache), the Jquery Part : 最后最好的(令人头疼的)是Jquery Part:

function clouds(cssDiv, otherOpened) {
var Div = "#Cloud_" + cssDiv;
var idButton = "#" + cssDiv;
var h = "200px";
if (otherOpened === null) {
    var clickedImg = "url(./images/" + cssDiv + ".jpg)";
    $(Div).fadeIn(1);
    $(Div).animate({
        height: h,
    }, 600);
    $(idButton).css({
        'box-shadow': ' inset 0px 4px 8px rgba(0,0,0,0.45)',
        'background-image': clickedImg
    });
    return Div;
} else {
    var buttonToUnclick = otherOpened.slice(7);
    var buttonToClick = cssDiv;
    var unClickedImg = "url(./images/" + buttonToUnclick + "Hover.jpg)";
    $(otherOpened).animate({
        height: "0",
    }, 600);
    $(otherOpened).fadeOut(1);
    $("#" + buttonToUnclick).css({
        'box-shadow': ' inset 0px 0px 0px rgba(0,0,0,0.45)',
        'background-image': unClickedImg
    });

    if (Div == otherOpened) {
        return null;
    } else {
        var clickedImg = "url(./images/" + buttonToClick + ".jpg)";
        setTimeout(function() {
            $(Div).fadeIn(1);
            $(Div).animate({
                height: h,
            }, 600);
            $("#" + buttonToClick).css({
                'box-shadow': ' inset 0px 4px 8px rgba(0,0,0,0.45)',
                'background-image': clickedImg
            });
            console.log(buttonToClick);
            console.log(clickedImg);
        }, 800);
        return Div;
    }
}
}

And its call : 及其调用:

$("#genre").click(function() {
    whichCloudsOn = clouds("genre", whichCloudsOn);

});
$("#artist").click(function() {
    whichCloudsOn = clouds("artist", whichCloudsOn);

});
$("#author").click(function() {
    whichCloudsOn = clouds("author", whichCloudsOn);

});
$("#year").click(function() {
    whichCloudsOn = clouds("year", whichCloudsOn);

});

Thanks for the help, I'm open to any advices or solutions. 感谢您的帮助,我欢迎任何建议或解决方案。

jQuery.css() adds inline styling, which will override your external css. jQuery.css()添加了内联样式,这将覆盖您的外部CSS。 Try adding !important on your hover, like this: 尝试在悬停上添加!important ,如下所示:

#year:hover {
    background-image:url(../images/year.jpg) !important;
}

I made an example fiddle . 我做出了表率小提琴 (click both and then hover) (同时单击并悬停)

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

相关问题 我点击一颗星后,JavaScript明星没有发光? - JavaScript stars aren't glowing after I've clicked a star? 隐藏div后如何显示它? - How can I make a div visible after I've hidden it? 单击按钮后,如何检查节点是否为jstree中的父节点? - How can I check if a node is a parent in jstree after I've clicked a button? 我想在输入后添加div if(hover或focus)然后如果我点击新div让div显示出来点击另一个元素或焦点 - i want to add div after input if (hover or focus) then if i clicked on the new div let the div displayed up to click on another element or focus 用javascript隐藏它后重新加载div? - Reload a div after i've hidden it with javascript? 在div被点击后,如何禁用点击事件? - How can I disable the click event after the div is clicked in react? 如何在单击div元素后显示更多文本? - How can I display further text after a div element is clicked? 如果不是 hover 或在一段时间后单击,如何隐藏和显示 div - how to hide and show a div if it is not hover or clicked after some time 如何隐藏我刚刚通过单击添加的 div? - How can I hide a div that I've just added with a click? 在将鼠标悬停在另一个元素上之后,如何使opacity = 1再保持5秒? - How can i keep opacity=1 5 more seconds after realeasing the hover on another element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM