简体   繁体   English

我应该如何在jQuery函数中使用CSS淡出

[英]How should I fade out with CSS in the jQuery function

I have this jQuery function 我有这个jQuery功能

 $(document).on('click', "a.delete-just-uploaded-selector", function() {
      $.ajax({
        type: "POST",
        url: url + "/path/",
        data: info,
        success: function(){

        }
    });
       $(this).parents(".deleteimage").animate("fast")
        .animate({ opacity: "hide" }, "slow");          
        return false;
    });

See my fade out animation in the last part. 在最后一部分中看到我的淡出动画。 I have read that it is better (faster and better for phones) using CSS transitions when it is simple animations insted of jQuery. 我已经读到,当它是由jQuery填充的简单动画时,使用CSS过渡会更好(对于手机来说越来越好)。 But how do I do it? 但是我该怎么办呢? Using .css() in jQuery? 在jQuery中使用.css() I have an external stylesheet. 我有一个外部样式表。 I know how to do CSS transitions in pure css but not the best way to combine it with jQuery. 我知道如何在纯CSS中进行CSS转换,但不是将其与jQuery结合的最佳方法。

If you want to do it with CSS it would be like 如果您想用CSS做到这一点,就像

@keyframes fade {
    from { opacity: 1;}
    to {  opacity: 0; }
}

div.fade {
    opacity: 0;
    animation-name: fade;
    animation-duration: 2s;
}

And when you want to hide an element you should only add class .fade 当您要隐藏元素时,您只应添加class .fade

$('button').on('click', function() {
    $('.my-div').addClass('fade');
});

See fiddle http://jsfiddle.net/shurshilin/cqbj02ob/1/ 参见小提琴http://jsfiddle.net/shurshilin/cqbj02ob/1/

You can use jquery fadeOut() . 您可以使用jquery fadeOut()

$( "p" ).click(function() {
  $( "p" ).fadeOut( "slow" );
});

Also you have different options available for css properties. 另外,您还有css属性可用的不同选项。

Eg : 例如:

specialEasing An object containing one or more of the CSS properties defined by the properties argument and their corresponding easing functions specialEasing一个对象,包含一个或多个由properties参数及其相应的缓动函数定义的CSS属性

More info jquery fade out 更多信息jQuery淡出

Try this: 尝试这个:

<style>
.deleteimage{
    background-color:red;
    width:100px;
    height:100px;
}
</style>

<div class="deleteimage">Fade</div>

<script>
//...
$( ".deleteimage" ).animate({
    opacity: 0
}, "slow");
</script>

You don't necessary have to use animation and define it like that. 您不必使用animation并像这样定义它。 You can just use transition : 您可以只使用transition

 $('.someLink').on('click', function() { $('.myDiv').toggleClass('hide'); }); 
 .myDiv { border: 1px solid black; padding: 10px; opacity: 1; line-height: 1em; transition: opacity 300ms ease, line-height 500ms ease, padding 500ms ease; -webkit-transition: opacity 300ms ease, line-height 500ms ease, padding 500ms ease; -o-transition: opacity 300ms ease, line-height 500ms ease, padding 500ms ease; -moz-transition: opacity 300ms ease, line-height 500ms ease, padding 500ms ease; } .myDiv.hide { opacity: 0; line-height: 0em; padding: 0px 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="myDiv">Boo</div> <a href="#" class="someLink">Click here</a> 

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

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