简体   繁体   English

jQuery上下动画

[英]jQuery animate up and down

I'm building a webpage with a grid of images and when the user click on an image, a transparant layer slide up with an animation with some info about the image. 我正在建立一个包含图像网格的网页,当用户单击图像时,透明层将滑动并显示带有有关图像信息的动画。 This is working fine with the script that I have made below. 我在下面编写的脚本可以很好地工作。

But I need to improve it because, when a user click on a second image, the prevoius transparant layer should slide back down and this is also working, but if the user click on the same layer, the animation will just slide up and then slide down. 但是我需要改进它,因为当用户单击第二个图像时,prevoius透明层应该向下滑动,这也可以正常工作,但是如果用户单击同一层,动画将只是向上滑动然后滑动下。 My question is if it's possible to improve my script to handle this. 我的问题是,是否有可能改善我的脚本以解决此问题。

My second question is that the script starts by slide down if any layer is already open, but does this affect all div tags or just the one that is opened? 我的第二个问题是,如果已经打开任何层,脚本将从下滑开始,但这会影响所有div标签还是仅影响打开的div标签? I guess there will be some performance issue if I have a big grid and the script slide down all layers despite that only one is visible!? 我想如果我有一个很大的网格并且尽管只有一层可见,脚本仍会向下滑动所有层,将会有一些性能问题! Are my code OK or does it have any flaws that could be improved? 我的代码是否还可以,或者是否有任何可以改进的缺陷?

JQuery: jQuery的:

$(document).ready(function(){
$(".outer").click(function(){ 
    $(".inner").animate({height:'-=100%'},250);
    $(this).children(".inner").animate({height:'+=100%'},250);
});
});

HTML: HTML:

<div class='outer'>
<img src="image-1.png" class="click">
<div class='inner'><h1>Cover 1</h1></div>
</div>
<div class='outer'>
<img src="image-2.png" class="click">
<div class='inner'><h1>Cover 2</h1></div>
</div>
<div class='outer'>
<img src="image-3.png" class="click">
<div class='inner'><h1>Cover 3</h1></div>
</div>

CSS: CSS:

.outer {
height: auto;
background: yellow;
position:relative;
width: 300px;
}

.outer img {
display: block;
}

.inner {
position: absolute;
bottom: 0;
height:0;
width: 100%;
background:rgba(0, 0, 0, 0.5);
}

Here's a short example of what you want to achieve. 这是您想要实现的简短示例。

$(".box").removeClass('animate');

It first removes all the class .animate on every .box , 它首先删除每个.animate上的所有.box类,

$(this).addClass('animate');

and after that, it sets that class only on the clicked element. 然后,它仅在clicked元素上设置该类。

The .animate class can be anything, as long as it only contains the changes to the normal state of the element. .animate类可以是任何东西,只要它仅包含对元素正常状态的更改即可。

The transition can be altered very easily. 过渡可以很容易地改变。 Transition comes in very handy: 过渡非常方便:

The transition CSS property is a shorthand property for transition-property , transition-duration , transition-timing-function , and transition-delay . transition CSS属性是transition-propertytransition-durationtransition-timing-functiontransition-delay的简写属性。 It enables you to define the transition between two states of an element. 它使您可以定义元素的两种状态之间的过渡。 Different states may be defined using pseudo-classes like :hover or :active or dynamically set using JavaScript. 可以使用伪类(如:hover:active定义不同的状态,或使用JavaScript动态设置。

Read more about it at Mozilla Developer Network . Mozilla开发人员网络上阅读有关它的更多信息。

Update 更新资料

    if ($(this).hasClass('animate')) {
        $(".box").removeClass('animate');
      } else {
        $(".box").removeClass('animate');
        $(this).addClass('animate');
      }

This will work for clicking another time. 这将在下次单击时起作用。

 $(".box").click(function(e) { e.preventDefault; if ($(this).hasClass('animate')) { $(".box").removeClass('animate'); } else { $(".box").removeClass('animate'); $(this).addClass('animate'); } }); 
 .box { background: red; width: 100px; height: 100px; margin: 10px; top: 0; transition: margin-left .5s ease-in-out; } .animate { margin-left: 100px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="box"></div> <div class="box"></div> <div class="box"></div> 

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

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