在JavaScript悬停时显示/隐藏div

时间:2013-12-19 11:23:22

标签: javascript html css

我希望图像叠加(带文本)仅在div悬停时显示。默认隐藏。如果合适,请使用JavaScript。

我尝试复制css,给出第一个(display:none)然后第二个用:hover和no(display:none),但是没有用,所以尝试用JavaScript添加/删除(display:none )。等级。

我添加了一个实时网址。我正在审视6个主页图片。

enter image description here enter image description here

实时网址:http://bit.ly/1jl1QaT

HTML

<div class="desc"><p><?=((strlen($r_main['friendlyTitle'])>40)?substr($r_main['friendlyTitle'],0,40).'...':$r_main['friendlyTitle']);?></p></div>
                </div></a>
                <a href="Shopping/"><div class="feature-2">
                    <div class="header"><p>Shopping</p></div>
                    <div class="desc"><p>Grab yourself a pair of Mink Fur Eyelashes for only £19.95 </p></div>
                </div></a>

CSS

#content-mid .col-2 .features .feature-1 .desc, #content-mid .col-2 .features .feature-2 .desc, #content-mid .col-2 .features .feature-3 .desc, #content-mid .col-2 .features .feature-4 .desc, #content-mid .col-2 .features .feature-5 .desc, #content-mid .col-2 .features .feature-6 .desc { position:absolute; width: 220px; height: 50px  zoom: 1; filter: alpha(opacity=90); opacity: 0.9; background: #333; bottom: 0; margin-bottom: 5px; display: none; }

#content-mid .col-2 .features .feature-1 .desc, #content-mid .col-2 .features .feature-2 .desc, #content-mid .col-2 .features .feature-3 .desc, #content-mid .col-2 .features .feature-4 .desc, #content-mid .col-2 .features .feature-5 .desc, #content-mid .col-2 .features .feature-6 .desc-show { position:absolute; width: 220px; height: 50px     zoom: 1; filter: alpha(opacity=90); opacity: 0.9; background: #333; bottom: 0; margin-bottom: 5px;  }

的JavaScript

$(".desc").hover(
      function () {
        $(this).removeClass("desc-show");
      },
      function () {
        $(this).addClass("desc");
      }
    );

4 个答案:

答案 0 :(得分:2)

HTML

<div style="background-color:red;">
<div id="blah">DEMO TEXT ON MOUSE OVER</div>
</div>

JAVA-SCRIPT

$('#blah').hover(function() {
    $(this).fadeTo(1,1);
},function() {
    $(this).fadeTo(1,0);
});

CSS

#blah {
    width:100px;
    height:100px;
    background-color:green;
    visibility: visible;
    opacity:0;
    filter:alpha(opacity=0);
}

Fiddle link

答案 1 :(得分:2)

#content-mid .col-2 .features .feature-1{
    border: 1px solid #E5E5E5;
    float: left;
    height: 160px;
    overflow: hidden;
    padding: 5px;
    position: relative;
    width: 220px;
    z-index:-1;
}
#content-mid .col-2 .features .image_holder_home_page{
    height: 160px;
    overflow: hidden;
    position: relative;
    width: 220px;
}

#content-mid .col-2 .features .feature-1 .header{
    background: none repeat scroll 0 0 #0098FF;
    height: 30px;
    position: absolute;
    width: 95.5%;
}

请将以上css代码添加到您的网站中。因为一些潜水元素溢出。这将解决这个问题。

答案 2 :(得分:0)

尝试this fiddle。我已经用了很多次了。

$(document).ready(function () {
                $(document).on('mouseenter', '.divbutton', function () {
                    $(this).find(":button").show();
                }).on('mouseleave', '.divbutton', function () {
                    $(this).find(":button").hide();
                });
            });

只需编辑此代码并将div替换为按钮。

答案 3 :(得分:0)

你的CSS错了。请使用此工具http://csslisible.com/en/让人类可读,您会看到“错误”的含义:

#content-mid .col-2 .features .feature-1 .desc,
#content-mid .col-2 .features .feature-2 .desc,
#content-mid .col-2 .features .feature-3 .desc,
#content-mid .col-2 .features .feature-4 .desc,
#content-mid .col-2 .features .feature-5 .desc,
#content-mid .col-2 .features .feature-6 .desc {
    display: none;
    position: absolute;
    bottom: 0;
    width: 220px;
    height: 50px zoom;
    margin-bottom: 5px;
    opacity: 0.9;
    background: #333;
    filter: alpha(opacity=90);
}

#content-mid .col-2 .features .feature-1 .desc, /* #TIP#: this is here */
#content-mid .col-2 .features .feature-2 .desc, /* #TIP#: this is here */
#content-mid .col-2 .features .feature-3 .desc, /* #TIP#: this is here */
#content-mid .col-2 .features .feature-4 .desc, /* #TIP#: this is here */
#content-mid .col-2 .features .feature-5 .desc, /* #TIP#: this is here */
#content-mid .col-2 .features .feature-6 .desc-show {
    position: absolute;
    bottom: 0;
    width: 220px;
    height: 50px zoom;
    margin-bottom: 5px;
    opacity: 0.9;
    background: #333;
    filter: alpha(opacity=90);
}

更轻松的事情:

  • 不需要JavaScript,
  • 减少CSS代码重复,
#content-mid .col-2 .features .feature-1 .desc,
#content-mid .col-2 .features .feature-2 .desc,
#content-mid .col-2 .features .feature-3 .desc,
#content-mid .col-2 .features .feature-4 .desc,
#content-mid .col-2 .features .feature-5 .desc,
#content-mid .col-2 .features .feature-6 .desc {
    display: none;
    position: absolute;
    bottom: 0;
    width: 220px;
    height: 50px zoom;
    margin-bottom: 5px;
    opacity: 0.9;
    background: #333;
    filter: alpha(opacity=90);
}

#content-mid .col-2 .features .feature-1:hover .desc,
#content-mid .col-2 .features .feature-2:hover .desc,
#content-mid .col-2 .features .feature-3:hover .desc,
#content-mid .col-2 .features .feature-4:hover .desc,
#content-mid .col-2 .features .feature-5:hover .desc,
#content-mid .col-2 .features .feature-6:hover .desc {
    display: block;
}



编辑:以下是JSFiddle的一个示例http://jsfiddle.net/pomeh/9XX46/

代码:

HTML:

<div class="features">

    <a href="Podcasts/">
        <div class="feature feature-1">
            <div class="header"><p>Podcasts</p></div>
            <div class="image_holder_home_page">
                <img src="http://lorempixel.com/220/159/city" />
            </div>
            <div class="desc"><p>Podcast Simple Page</p></div>
        </div>
    </a>

    <a href="Shopping/">
        <div class="feature feature-2">
            <div class="header"><p>Shopping</p></div>
            <div class="image_holder_home_page">
                <img src="http://lorempixel.com/220/159/food" />
            </div>
            <div class="desc"><p>Grab yourself a pair of Mink Fur Eyelashes for only £19.95 </p></div>
        </div>
    </a>

    <a href="Confessions/">
        <div class="feature feature-3">
            <div class="header"><p>Confessions</p></div>
            <div class="image_holder_home_page">
                <img src="http://lorempixel.com/220/159/sports" />
            </div>
            <div class="desc"><p>tttttttttttttttt</p></div>
        </div>
    </a>

</div>

CSS:

.features .feature {
    width: 220px;
    height: 50px zoom;
    margin-bottom: 5px;
    position: relative;
}

p {
    padding: 0;
    margin: 0;
}

.features .feature .header {
    width: 100%;
    text-transform: uppercase;
    color: white;
    margin: 0;
}
.features .feature-1 .header {
    background-color: blue;
}
.features .feature-2 .header {
    background-color: red;
}
.features .feature-3 .header {
    background-color: green;
}

.features .feature .desc {
    display: none;
    opacity: 0.9;
    background: #333;
    width: 220px;
    filter: alpha(opacity=90);
    margin: 0;
    padding: 0;
    position: absolute;
    bottom: 5px;
}

.features .feature:hover .desc {
    display: block;
}