[英]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;
p
在div
内的位置在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. 您需要担心的一件事是, img
, p
等的跨度不会超过div高度的两倍,因为那样一来,您始终会丢失中间的内容,因为它的内容会从隐藏在下方变为隐藏在上方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.