javascript - jQuery effect .fadeTo() with toggle? - Stack Overflow

I want to know the simplest way to toggle the .fadeTo() effect when someone click again on Click Me! bu

I want to know the simplest way to toggle the .fadeTo() effect when someone click again on Click Me! button then it will reverse the process again. I mean just like the .fadeToggle() works or something like that. But using .fadeToggle() cause the div to disappear.

Here is my - JSFiddle

HTML

<button>Click Me!</button>
<div></div>

CSS

div {
    background-color: #ff0000;
    width: 200px;
    height: 200px;
    margin: 20px
}

jQuery

$(document).ready(function(){
  $("button").click(function(){
  $("div").fadeTo(400,0.4);
  });
});

I want to know the simplest way to toggle the .fadeTo() effect when someone click again on Click Me! button then it will reverse the process again. I mean just like the .fadeToggle() works or something like that. But using .fadeToggle() cause the div to disappear.

Here is my - JSFiddle

HTML

<button>Click Me!</button>
<div></div>

CSS

div {
    background-color: #ff0000;
    width: 200px;
    height: 200px;
    margin: 20px
}

jQuery

$(document).ready(function(){
  $("button").click(function(){
  $("div").fadeTo(400,0.4);
  });
});
Share Improve this question asked Apr 23, 2014 at 13:33 AnonymousAnonymous 10.2k3 gold badges26 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You could use following snippet:

DEMO

$(document).ready(function () {
    $("button").click(function () {
        this.toggle = !this.toggle;
        $("div").stop().fadeTo(400, this.toggle ? 0.4 : 1);
    });
});

Or usually better solution is to toggle a class instead:

DEMO

CSS:

div {
    background-color: #ff0000;
    width: 200px;
    height: 200px;
    margin: 20px;
    -webkit-transition: opacity 400ms linear;
    transition: opacity 400ms linear;    
}

div.faded {
    opacity:.4;
}

jQuery:

$(document).ready(function () {
    $("button").click(function () {
        $("div").toggleClass('faded');
    });
});

Much simpler ,

 <button>fadeToggle p1</button>
 <p>This paragraph has a slow, linear fade.</p>
  <script>
   $( "button:first" ).click(function() {
     $( "p:first" ).fadeToggle( "slow", "linear" );
   });
 </script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744241565a4564728.html

相关推荐

  • javascript - jQuery effect .fadeTo() with toggle? - Stack Overflow

    I want to know the simplest way to toggle the .fadeTo() effect when someone click again on Click Me! bu

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信