简体   繁体   English

隐藏一个div并显示另一个div时使用jQuery进行幻灯片过渡

[英]Using jQuery to do a slide transition when hiding one div and displaying another overtop of it

I am attempting to create a slide up effect using jQuery or pure css. 我正在尝试使用jQuery或纯CSS创建向上滑动效果。

I have a container div and then 2 content divs inside. 我有一个容器div,然后在其中有2个内容div。 I would like to achieve an effect like in the first tile in this demo on mouse hover and mouse out. 我想在鼠标悬停和鼠标移出时获得类似于本演示中的第一个图块的效果。

Please see this https://jsfiddle.net/jfyacwvg/ of my current code. 请查看我当前代码的https://jsfiddle.net/jfyacwvg/ You will notice that when hovering over Item 1 the paragraph appears. 您会注意到,将鼠标悬停在项目1上时,将出现该段落。 This is fine but I want it to appear with some sort of slide effect. 很好,但是我希望它以某种幻灯片效果出现。

Please do not mind the fact that Item 2 and 3 change when hovering Item 1, I will have to fix that later on. 请不要介意第2项和第3项在悬停第1项时发生变化,我稍后将不得不对其进行修复。

Here is my JavaScript code to hide show, but please see the jsfiddle link for everything: 这是我要隐藏显示的JavaScript代码,但是请查看jsfiddle链接以获取所有信息:

$(".card").hover(
  function () {
      $(".content-1").hide();
      $(".content-2").show();
  }, function() {
      $(".content-2").hide();
      $(".content-1").show();
    }
);

Here is the html: 这是html:

<div class="container">

<div class="row">
    <div class="col-md-10 col-md-offset-1">
        <div class="row">
            <div class="col-md-4">
                <div class="card">
                    <div class="content-1" style="height: 300px">
                     <img src="http://placehold.it/350x150" class="img-responsive" style="margin-left: auto; margin-right: auto;" />
                     <h3 class="text-center">Item 1</h3>
                    </div>
                    <div class="content-2" style="display: none; height: 300px;">
                        <p style="margin:30px 0 30px 0">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card">
                    <div class="content-1" style="height: 300px">
                     <img src="http://placehold.it/350x150" class="img-responsive" style="margin-left: auto; margin-right: auto;" />
                     <h3 class="text-center">Item 2</h3>
                    </div>
                    <div class="content-2" style="display: none; height: 300px;">
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                                <div class="card">
                    <div class="content-1" style="height: 300px">
                     <img src="http://placehold.it/350x150" class="img-responsive" style="margin-left: auto; margin-right: auto;" />
                     <h3 class="text-center">Item 3</h3>
                    </div>
                    <div class="content-2" style="display: none; height: 300px;">
                        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
    </div>

It's just change the hide/show for slideUp/slideDown: 只是更改了slideUp / slideDown的隐藏/显示:

        $(".card").hover(
      function () {
          $(".content-1").slideUp();
          $(".content-2").slideDown();
      }, function() {
          $(".content-2").slideUp();
          $(".content-1").slideDown();
        }
    );

Your fiddle updated: 您的小提琴已更新:

https://jsfiddle.net/jfyacwvg/2/ https://jsfiddle.net/jfyacwvg/2/

Look at this JSFiddle, it is done in CSS. 看看这个JSFiddle,它是用CSS完成的。

https://jsfiddle.net/de8L2f1L/ https://jsfiddle.net/de8L2f1L/

The position of the p inside the div is at the top: 0px; pdiv内的位置在top: 0px; just as it normally would be. 像往常一样。 When you hover over the p it then moves up, top: -200px with a transition of 2 seconds (which can be changed). 当您将鼠标悬停在p它会向上移动top: -200px ,过渡为2秒(可以更改)。 The reason it is set to change to -200px is because the height of your div is 200px. 之所以将其设置为-200px,是因为div的高度为200px。

overflow: hidden was applied to the div to keep it from expanding with the size of the p . overflow: hidden对div应用了overflow: hidden ,以防止其随着p的大小而扩展。

One thing you need to worry about is that your img , p , etc do not span more than twice height of the div because you would then always be missing the content in the middle as it would go from being hidden below to being hidden above. 您需要担心的一件事是, imgp等的跨度不会超过div高度的两倍,因为那样一来,您始终会丢失中间的内容,因为它的内容会从隐藏在下方变为隐藏在上方。

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

相关问题 如何排序一个div的隐藏顺序,然后使用jQuery动画显示另一个div? - How to sequence the hiding of one div then show another using jQuery animations? 使用jQuery的上移过渡功能将div中的内容替换为其他内容 - Replace content in div with other content using a slide up transition with jQuery 隐藏一个div以使用jQuery显示另一个 - Hiding a div to show another using jQuery 如何使用JQuery UI幻灯片效果无缝滑出一个div并在另一个div中滑动? - How to use JQuery UI slide effect to seamlessly slide out one div and slide in another div? 隐藏3列并将一列扩展为行的100%时的jQuery过渡效果 - jQuery transition effect when hiding 3 columns and expanding one to be 100% of row 使用jQuery将文本值隐藏在一个列表中,同时在另一个列表中显示其值 - Hiding a text value in one list while displaying its value in another using jquery 使用 Jquery 在 CodeIgniter 中基于下拉选择隐藏/显示 Div - Hiding / Displaying Div Based on Dropdown Selection in CodeIgniter Using Jquery 使用jQuery UI滑动隐藏div。 如何将其相邻的div与其一起正确滑动以取代它的位置? - Slide Hiding a div with jQuery UI. How do I slide its adjacent div right along with it to take its place? 使用JQuery隐藏div - Hiding a div using JQuery 使用jQuery将一张图片过渡到另一张图片 - Transition one image to another using jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM