简体   繁体   English

在悬停时隐藏图像标题并显示覆盖文字

[英]Hide image caption on hover and show overlay text

Hide image caption on hover and show overlay text: I have image which has title(caption) on it. 在悬停时隐藏图像标题并显示覆盖文本:我的图像上面带有标题(标题)。 What I'm trying to do now is when I hover over the image to remove the title and show other text (description). 我现在想做的是将鼠标悬停在图像上以删除标题并显示其他文本(说明)。

The problem is that the title is hidden but also the other text. 问题在于标题是隐藏的,但其他文本也是隐藏的。

Can someone point me where I'm wrong here? 有人可以指出我在哪里错了吗?

 $(document).ready(function() { $('.overlay').hide(); $('.section-solution').hover( function() { $('.hide-on-hover').hide(); $('.text').show(); }, function() { $('.hide-on-hover').show(); $('.text').hide(); } ); }); 
 .container-img { position: relative; padding: 20px; } .wp-caption { position: relative; padding: 0; margin: 0; } .fullwidth-img img { margin-bottom: 70px; width: 100%; height: 400px; object-fit: cover; } .wp-caption img { display: block; max-width: 100%; height: auto; } .wp-caption-text { display: block; position: absolute; width: 100%; color: #fff; left: 0; bottom: 0; padding: 4em; font-weight: 700; z-index: 2; -webkit-box-sizing: border-box; box-sizing: border-box; } .wp-caption-text p { color: white; font-size: 26px; font-weight: 500; } .overlay { position: absolute; top: 0; bottom: 0; left: 0; right: 0; width: 100%; opacity: 0; transition: .5s ease; background-color: black; color: white; font-size: 20px; padding: 2em; } .container-img:hover .overlay { opacity: 1; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <section class="section-solution "> <div class="container"> <div class="row"> <div class="container-img fullwidth-img" id="last"> <figure class="wp-caption"> <div class="demo"> <img width="300" height="200" src="https://img.purch.com/w/660/aHR0cDovL3d3dy5saXZlc2NpZW5jZS5jb20vaW1hZ2VzL2kvMDAwLzEwMC8zNDkvb3JpZ2luYWwvc2liZXJpYW4tdGlnZXIuanBn"> </div> <div class="overlay"> <div class="text"> <figcaption class="wp-caption-text on-hover">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</figcaption> </div> </div> <figcaption class="wp-caption-text"> <p class="hide-on-hover"> Lorem ipsum </p> </figcaption> </figure> </div> </div> </div> </section> 

The fiddle https://jsfiddle.net/98j07g4k/1/ 小提琴https://jsfiddle.net/98j07g4k/1/

Another possible way is to hide the title through the CSS. 另一种可能的方法是通过CSS隐藏标题。 You can remove the JS part also. 您也可以删除JS部分。 Simply target class hide-on-hover in your CSS and add display: none; 只需将目标hide-on-hoverhide-on-hover在CSS中并添加display: none;

Here is how would look like 这是什么样子

 .container-img { position: relative; padding: 20px; } .wp-caption { position: relative; padding: 0; margin: 0; } .fullwidth-img img { margin-bottom: 70px; width: 100%; height: 400px; object-fit: cover; } .wp-caption img { display: block; max-width: 100%; height: auto; } .wp-caption-text { display: block; position: absolute; width: 100%; color: #fff; left: 0; bottom: 0; padding: 4em; font-weight: 700; z-index: 2; -webkit-box-sizing: border-box; box-sizing: border-box; } .wp-caption-text p { color: white; font-size: 26px; font-weight: 500; } .overlay { position: absolute; top: 0; bottom: 0; left: 0; right: 0; width: 100%; opacity: 0; transition: .5s ease; background-color: black; color: white; font-size: 20px; padding: 2em; } .container-img:hover .overlay { opacity: 1; } .container-img:hover .hide-on-hover { display: none; } 
 <section class="section-solution "> <div class="container"> <div class="row"> <div class="container-img fullwidth-img" id="last"> <figure class="wp-caption"> <div class="demo"> <img width="300" height="200" src="https://img.purch.com/w/660/aHR0cDovL3d3dy5saXZlc2NpZW5jZS5jb20vaW1hZ2VzL2kvMDAwLzEwMC8zNDkvb3JpZ2luYWwvc2liZXJpYW4tdGlnZXIuanBn"> </div> <div class="overlay"> <div class="text"><figcaption class="wp-caption-text on-hover"><p class="show_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p></figcaption></div> </div> <figcaption class="wp-caption-text"> <p class="hide-on-hover"> Lorem ipsum </p> </figcaption> </figure> </div> </div> </div> </section> 

Probably the simplest and fastest way which I can think of. 我想到的可能是最简单,最快的方法。

Remove $('.overlay').hide(); 删除$('.overlay').hide(); from the code it will work. 从代码它将起作用。

 $(document).ready(function() { $('.section-solution').hover( function() { $('.hide-on-hover').hide(); $('.show_text').show(); }, function() { $('.hide-on-hover').show(); $('.show_text').hide(); } ); }); 
 .container-img { position: relative; padding: 20px; } .wp-caption { position: relative; padding: 0; margin: 0; } .fullwidth-img img { margin-bottom: 70px; width: 100%; height: 400px; object-fit: cover; } .wp-caption img { display: block; max-width: 100%; height: auto; } .wp-caption-text { display: block; position: absolute; width: 100%; color: #fff; left: 0; bottom: 0; padding: 4em; font-weight: 700; z-index: 2; -webkit-box-sizing: border-box; box-sizing: border-box; } .wp-caption-text p { color: white; font-size: 26px; font-weight: 500; } .overlay { position: absolute; top: 0; bottom: 0; left: 0; right: 0; width: 100%; opacity: 0; transition: .5s ease; background-color: black; color: white; font-size: 20px; padding: 2em; } .container-img:hover .overlay { opacity: 1; } 
 <section class="section-solution "> <div class="container"> <div class="row"> <div class="container-img fullwidth-img" id="last"> <figure class="wp-caption"> <div class="demo"> <img width="300" height="200" src="https://img.purch.com/w/660/aHR0cDovL3d3dy5saXZlc2NpZW5jZS5jb20vaW1hZ2VzL2kvMDAwLzEwMC8zNDkvb3JpZ2luYWwvc2liZXJpYW4tdGlnZXIuanBn"> </div> <div class="overlay"> <div class="text"><figcaption class="wp-caption-text on-hover"><p class="show_text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p></figcaption></div> </div> <figcaption class="wp-caption-text"> <p class="hide-on-hover"> Lorem ipsum </p> </figcaption> </figure> </div> </div> </div> </section> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Swap your javascript file for this and I'll add comments in the code for understanding. 为此交换您的javascript文件,我将在代码中添加注释以供理解。

  $(document).ready(function() {

        var trade = true; // variable that controls whether the mouse was on 
                          // top of the image and left.

        $('.overlay').hide();
            $('.section-solution').hover(
                function () {
                    if(trade) {
                      $('.hide-on-hover').html("test");
                      $('.text').show();
                      trade = false;
                    } else {
                        // Here I set new text that can be any text just 
                        // need pass to html.
                        $('.hide-on-hover').html("Lorem ipsum");
                      trade = true;
                    }
                }
            );

         });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM