在单击和鼠标悬停时更改元素的颜色

时间:2014-11-20 21:14:34

标签: javascript jquery css onclick mouseover

我正在尝试编写一个代码,使得一个元素在点击时使用更高的z-index到达前面,我似乎已经想到了,并且我试图使该元素保持不同颜色将其标识为当前页面。我想允许鼠标悬停功能虽然改变其他元素的颜色,以将它们标识为其他按钮,这些元素看起来像文件标签,所以这样看起来更好。不幸的是,我添加了一个javascript代码,以便在单击另一个元素时元素将返回到原始渐变颜色,但我似乎无法弄清楚如何使鼠标悬停功能适用于每个单独的元素。我不知道这对你有意义。

使用Javascript:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
    $(function() {
    // Change this selector to find whatever your 'boxes' are
    var boxes = $("div.menuitem");

    // Set up click handlers for each box
    boxes.click(function() {
        var el = $(this), // The box that was clicked
            max = 0;

        // Find the highest z-index
        boxes.each(function() {
            // Find the current z-index value
            var z = parseInt( $( this ).css( "z-index" ), 10 );
            // Keep either the current max, or the current z-index, whichever is higher
            max = Math.max( max, z );
        });

        // Set the box that was clicked to the highest z-index plus one
        el.css("z-index", max + 1 );
    });
});
    $(document).ready(function()
        {


    $("#productmenu").mouseover(function() { $("#productdropdown").css('visibility','visible'); });
    $("#productdropdown").mouseover(function() { $("#productdropdown").css('visibility','visible'); });
    $("#productdropdown").mouseout(function() { $("#productdropdown").css('visibility','hidden'); });
    $("#productmenu").mouseout(function() { $("#productdropdown").css('visibility','hidden'); }); 

        });
    $(document).ready(function()
        {


    $("#productmenu").click(function() { $("#couponmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#productmenu").click(function() { $("#contestmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#productmenu").click(function() { $("#contactmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#couponmenu").click(function() { $("#productmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#couponmenu").click(function() { $("#contestmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#couponmenu").click(function() { $("#contactmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#contestmenu").click(function() { $("#couponmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#contestmenu").click(function() { $("#productmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#contestmenu").click(function() { $("#contactmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#contactmenu").click(function() { $("#productmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#contactmenu").click(function() { $("#contestmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });
    $("#contactmenu").click(function() { $("#couponmenu").css('background','linear-gradient(0deg, #F90, #D76B00)'); });

        })
        </script>

我包含了一个像这样的CSS,每个元素,但javascript阻止它工作:

#productmenu:hover {
    background:linear-gradient(0deg, #FF972F, #FFD3A8);
    z-index:500;
}

HTML:

<a href="#">
<div id="productmenu" class="menuitem" onclick="this.style.background='linear-gradient(0deg, #FF972F, #FFD3A8)',style.zIndex='500';">Products</div>
<ul id="productdropdown">
<li><a href="#">Product 1</a></li>
<li><a href="#">Product 2</a></li>
<li><a href="#">Product 3</a></li>
</ul>
</a>
<a href="#">
<div id="contestmenu" class="menuitem" onclick="this.style.background='linear-gradient(0deg, #FF972F, #FFD3A8)',style.zIndex='500';">Contest</div>
</a>
<a href="#">
<div id="couponmenu" class="menuitem" onclick="this.style.background='linear-gradient(0deg, #FF972F, #FFD3A8)',style.zIndex='500';">Coupon</div>
</a>
<a href="#">
<div id="contactmenu" class="menuitem" onclick="this.style.background='linear-gradient(0deg, #FF972F, #FFD3A8)',style.zIndex='500';">Contact</div>
</a>

1 个答案:

答案 0 :(得分:0)

你去男人吧。花了一些时间来弄清楚你想要的是什么。您的HTML和CSS需要一些工作,因为它绝对不符合最佳实践。

Here's a fiddle with it working

我们放弃了<a>标记,不包裹您的div。您不应该在将来这样做,因为<a>元素是内联的,而div是块。

<强> HTML

<div id="productmenu" class="menuitem"><a>Products</a>
    <ul id="productdropdown">
        <li><a href="#">Product 1</a></li>
        <li><a href="#">Product 2</a></li>
        <li><a href="#">Product 3</a></li>
    </ul>
</div>  

我们还摆脱了很多JavaScript,因为大部分内容可以通过CSS更好地完成,所以我们删除了整个部分:

<强>的JavaScript

$(document).ready(function() {
  $("#productmenu").mouseover(function() { $("#productdropdown").css('visibility','visible'); });
  $("#productdropdown").mouseover(function() { $("#productdropdown").css('visibility','visible'); });
  $("#productdropdown").mouseout(function() { $("#productdropdown").css('visibility','hidden'); });
  $("#productmenu").mouseout(function() { $("#productdropdown").css('visibility','hidden'); }); 
});

整个事情都消失了,因为大多数情况都没有意义。

这是将要接管大部分辛勤工作的CSS:

<强> CSS

[id$=menu]:hover > a,
[id$=menu] li:hover > a {
    background:linear-gradient(0deg, #FF972F, #FFD3A8);
}
[id$=menu]:hover ul {
    display: block;  
    list-style: none;
    margin: 0;
    padding: 0 0 0 20px;
}
[id$=menu] ul {
    display: none;
}
[id$=menu] a {
    display: block;
}