简体   繁体   English

jQuery在单击时改进.css()动画

[英]Jquery improve .css() animation on click

I have this code to make an animation when some images are clicked. 单击某些图像时,我有这段代码可以制作动画。 It will manage the css properties to hide or show the selected contents. 它将管理css属性以隐藏或显示所选内容。 I've noticed that the animation is not smooth as I expected. 我注意到动画不像我预期的那样平滑。 Is there a way to improve this code? 有没有办法改善此代码?

<script>
$(document).ready(function(){
open_panels();
close_panels();

});
// speed up 
var body = $('body');
var panel1 = $('#panel1');
var panel2 = $('#panel2');
var panel3 = $('#panel3');
var panel1link = $('#panel1-link');
var panel2link = $('#panel2-link');
var panel3link = $('#panel3-link');

function close_panels(){
$('[id="close-panel"]').on('click',function(e){
panel1.css({marginLeft:'-2em',width:0});
panel2.css({marginLeft:'-2em',width:0});
panel3.css({marginLeft:'-2em',width:0});
body.css('overflow','auto');
});
}

function open_panels(){
panel1link.on('click',function(e){
e.preventDefault();
panel1.css({marginLeft:0,width:"100%"});
body.css('overflow','hidden');
});

panel2link.on('click',function(e){
e.preventDefault();
panel2.css({marginLeft:0,width:"100%"});
body.css('overflow','hidden');
});

panel3link.on('click',function(e){
e.preventDefault();
panel3.css({marginLeft:0,width:"100%"});
body.css('overflow','hidden');
});

}
</script>

In my personal opinion, instead of doing it in jQuery like this, you should use CSS's @keyframes rule. 我个人认为,而不是像这样在jQuery中执行此操作,应该使用CSS的@keyframes规则。

Here is an example of how you would do this: 以下是您如何执行此操作的示例:

@keyframes animationName {
    from { background-color: red; }
    to { background-color: yellow; }
}

This creates an animation that all change the background color from red to yellow, but will work with any CSS property. 这将创建一个动画,将所有背景颜色从红色更改为黄色,但可与任何CSS属性一起使用。

Once you put an @keyframes rule in your CSS file, you can apply the animation to your selected element using the .css in jQuery. 将@keyframes规则放入CSS文件后,即可使用jQuery中的.css将动画应用于所选元素。

Make sure to specify the name of your animation, and the duration. 确保指定动画的名称和持续时间。

animation-name: animationName;
animation-duration: 4s;

I hope this helps, if not feel free to ask me for more information! 希望这对您有所帮助,如果可以随时向我询问更多信息!

PS Indent your code, it makes it a million times easier to read ;) PS缩进您的代码,使其可读性提高一百万倍;)

When I was asking for this not real issue, I was not considering the possibility to achieve a smooth animation using the CSS rules. 当我问这个不是真正的问题时,我没有考虑使用CSS规则实现平滑动画的可能性。 After reading the suggestions in the comments and the answer to this question, I've found a solution that is working nice. 阅读评论中的建议和该问题的答案后,我发现了一个不错的解决方案。 here is how i changed the code: 这是我如何更改代码:

CSS code: CSS代码:

<style>
#panel1,#panel2,#panel3 {
  height: 100%;
  width: 100%;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  margin: 0;
  background-color: #dfab3d;
  overflow-x: hidden;
  transform: translateX(-100%);
  transition: all 300ms;
}

</style>

Jquery code: jQuery代码:

<script>
$(document).ready(function(){
  open_panels();
  close_panels();
});

var body = $('body');
var panel1link = $('#panel1-link');
var panel2link = $('#panel2-link');
var panel3link = $('#panel3-link');
var panel1 = $('#panel1');
var panel2 = $('#panel2');
var panel3 = $('#panel3');

function close_panels(){
  $('[id="close-panel"]').on('click',function(e){
    panel1.css({transform: 'translateX(-100%)'});
    panel2.css({transform: 'translateX(-100%)'});
    panel3.css({transform: 'translateX(-100%)'});
    body.css('overflow','auto');
  });
}

function open_panels(){
  panel1link.on('click',function(e){
    e.preventDefault();
    panel1.css({transform: 'translateX(0%)'});
    body.css('overflow','hidden');
  });

  panel2link.on('click',function(e){
    e.preventDefault();
    panel2.css({transform: 'translateX(0%)'});
    body.css('overflow','hidden');
  });

  panel3link.on('click',function(e){
    e.preventDefault();
    panel3.css({transform: 'translateX(0%)'});
    body.css('overflow','hidden');
  });
}
</script>

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

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