最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - DisplayHide a div on JavaScript hover - Stack Overflow

programmeradmin2浏览0评论

I want the image overlay (with text) to only display when the div is hovered over. Hidden by default. By JavaScript if suitable.

I tried duplicating the css, giving the first (display: none) and then the second with :hover and no (display: none), but did not work, so trying with JavaScript to add/remove the (display: none) class.

I've included a live URL. I am reffering to the 6 homepage images.

Live URL:

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");
      }
    );

I want the image overlay (with text) to only display when the div is hovered over. Hidden by default. By JavaScript if suitable.

I tried duplicating the css, giving the first (display: none) and then the second with :hover and no (display: none), but did not work, so trying with JavaScript to add/remove the (display: none) class.

I've included a live URL. I am reffering to the 6 homepage images.

Live URL: 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");
      }
    );
Share Improve this question asked Dec 19, 2013 at 11:23 wharfdalewharfdale 7627 gold badges24 silver badges60 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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

#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%;
}

Please add above css code into your site. because some dive elements are overflowing. this will fix that issue.

try this fiddle. I have used it so many time.

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

just edit this code and put your div instead of button.

You're CSS is wrong. Please use this tool http://csslisible./en/ to get it readable by an human, and you'll see what I mean by "wrong":

#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);
}

What about something way easier:

  • no JavaScript required,
  • less CSS code duplication,
#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;
}



EDIT: Here's an example on JSFiddle http://jsfiddle/pomeh/9XX46/

The code:

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./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./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./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;
}
发布评论

评论列表(0)

  1. 暂无评论