简体   繁体   中英

Changing the hide show toggle effect?

I have an element that works just fine with the following code. It's an object #obj1 that is hidden when loading the page, but appears when clicking on #obj2.

#obj1{
    position:fixed;  
    width:100px;  
    bottom:180px;
    right:100px; 
    display:none;
}

$("#obj1").hide();
$("#obj2").show();$('#obj2').toggle(function(){
$("#obj1").slideDown(function(){});
},function(){
$("#obj1").slideUp(function(){});
});

but I would like to have it like this:

$("#obj1").css({"opacity": "0","bottom": "180"})
$("#obj2").toggle(
function () {
$("#obj1").animate({"opacity": "1","bottom": "140"}, "slow");
},function () {
$("#obj1").animate({"opacity": "0","bottom": "180"}, "slow");
});

I would like it to fade in, but how do I add the animation to the first script? (animation ex: .animate({"opacity": "1","bottom": "140"}, "slow");)

Here is a super simple demo of fading in an element using CSS. You can use jQuery to add the class through a click event.

// HTML
<div id="myId" class="hide">
  This is div with myId
</div>

// CSS
.hide {
  display: none;
}
.myId {
  animation: fadein 2s;
}

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

// JQUERY
$("#myId").removeClass("hide").addClass("myId");

You can see a working demo here . You'll just have to modify it to trigger on click of obj2 or where you like

EDIT - As per your comment above I have edited the pen, so now the element will be hidden on page load and then the class will be removed and the animation class added.

You would be best keeping the styles within css, and just using js to change the state (add/remove a class). The way you have the javascript is passable, but it'd be better for the class to be toggled based on itself so they can't accidentally get out of sync:

 $('#obj2').on('click',function(e){ e.preventDefault(); if($('#obj1').hasClass('js-on')) $('#obj1').removeClass('js-on'); else $('#obj1').addClass('js-on'); }); 
  #obj1{ position:absolute; width:100px; bottom:10px; right:20px; opacity: 0; background-color: yellow; padding: 1em; transition: .5s opacity, .5s bottom; } #obj1.js-on { opacity: 1; bottom: 40px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="obj2" href="#">Click me</a> <div id="obj1">Hi</div> 

$(document).ready(function() {
$("#obj1").hide();
$("#obj2").show();
});

$('#obj2').toggle(function(){
$("#obj1").slideToggle();
});

This will show obj1 by sliding when obj2 is pressed. To have it fade in instead Try,

$("#obj2").click(function () {
$("#obj1").fadeToggle("slow","swing");

This toggles obj1 fading in and out.

reference: http://api.jquery.com/fadetoggle/

Slightly confused by the question, but here's my attempt at an answer: hope it helps

 $(".obj1").click(function(){ $(".obj2").css('opacity', 0) .slideDown('slow') .animate( { opacity: 1 }, { queue: false, duration: 'slow' } ); }); 
 .obj1 { display: inline-block; padding: 10px; background: lightgrey; } .obj2 { height: 100px; width: 100px; background: red; display:none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="obj1">click me</div> <div class="obj2"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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