How to achieve a slideToggle effect with jQuery?

Asked

Viewed 855 times

2

In this link, i have red stripes in the specialty. I would like to ask what effect I will use for that one div replace the contents of individual stripes with a type effect slideToggle from left to right. How to start this script in jQuery?

  • I didn’t quite understand the question. But, you can use transitions in the CSS and background-position.

1 answer

3


Start with something like this:

jquery:

$('.disparador').click(function () {
    $(this).animate({
        width: ($(this).width() == 200 ? 100 : 200)
    });
    $(this).siblings('.painel').animate({
        width: 'toggle'
    });
});

HTML:

<div class='container'>
    <div class='painel'>Lorem ipsum dolor sit amet.</div>
    <div class='disparador'></div>
</div>

<div class='container'>
    <div class='painel'>Lorem ipsum dolor sit amet.</div>
    <div class='disparador'></div>
</div>

<div class='container'>
    <div class='painel'>Lorem ipsum dolor sit amet.</div>
    <div class='disparador'></div>
</div>

CSS:

.container {
    position: relative;
    width:200px;
    height: 350px;
    float: left;
}
.painel {
    display:none;
    width: 100px;
    height: 350px;
    overflow: auto;
    position:absolute;
    top: 0;
    left:0;
}
.disparador {
    width: 200px;
    min-width: 50%;
    height: 350px;
    position:absolute;
    top: 0;
    right:0;
    background-color: red;
}

Jsfiddle

  • $('#widget_sp_image-2, #widget_sp_image-3, #widget_sp_image-4').click(function (e) { ... I have 3 different ids ... how would apply the panel $('.painel').animate({width: "toggle"}, 750); for your specific id?

  • This is the magic of jquery, create a mechanism to point to the desired element, using data- or any other parameter...

  • @marcosvinicius I edited the answer, now it’s referencing the clicked element and its panel. see working on jsfiddle...

  • maybe it is necessary to concatenate a string "px" because of the unit of measure, sometimes it works without, but it would be a good practice to put

  • @leandro17br according to the manual every numerical value will be treated as pixels, so I think there is no need to complicate...

  • I managed to do an effect. Many of the times I have difficulty making the script turn, like ... it GOES but how does it TURN? I don’t know if you understand, something related to the inverse of what has just been done. I think this is programming logic, no?

  • 1

    @marcosvinicius To do and undo, prefer to use toggle when available, it always reverses the action automatically, ie if this hidden it shows, if this visible it hides. When what you want to do does not provide a toggle option, you need to check the current situation and apply the reverse, as in my ex. I want to toggle the width of the trigger between 200 and 100, see that I put an if width == 200 returns 100 if it does not return 200

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.