简体   繁体   中英

Slide Div up and down on Click

On click of a tag the div opens and close accordingly. What i want is that the div should slide down and up slowly instead of showing up immediately.

<a href="" class="accordion">Click here</a>
 <div class="panel" id="AccordionDiv">
            <div class="store">
                <div class="store-row">

                    <div class="cells store-logo text-center">
                        <img src="@strStaticWebsiteUrl@(objOfferPrice.StoreImage)" alt="" />
                    </div>

                    @if (objOfferPrice.Price < objOfferPrice.UrlPrice)
                    {
                        <div class="cells text-center">
                            <div class="product-price offer-price">Rs. @String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0,0}", objOfferPrice.Price)<sup>*</sup></div>
                            <p class="real-price">Price: @objOfferPrice.UrlPrice</p>
                        </div>
                    }
                </div>
           </div>

This is html code and below is the Script code.

            <script>
                var acc = document.getElementsByClassName("accordion");
                var i;
                for (i = 0; i < acc.length; i++) {
                    acc[i].addEventListener("click", function () {

                        this.classList.toggle("active");
                        var panel = this.nextElementSibling;
                        if (panel.style.display === "block") {
                            panel.style.display = "none";
                        } else {
                            panel.style.display = "block";
                        }
                    });
                        }
            </script>

There is a slideToggle method that jQuery provides for this effect

var panel = this.nextElementSibling;
$(panel).slideToggle();

Yes we have Animation in JQuery "slideDown", slideUp and slideToggle.

$( "#ID1" ).click(function() {
  $( "#book" ).slideDown( "slow", function() {
    // Animation slow slideDown.
  });
});

Same we can use with slideUp.

And "slideToggle" is for Up and Down both.

Use CSS

CSS animations look a lot better than Javascript animation, also. CSS animations can be canceled/reversed half way.

Javascript:

<script>
    var acc = document.getElementsByClassName("accordion");
    for (i = 0; i < acc.length; i++) {
        acc[i].addEventListener("click", function () {
            this.classList.toggle("active");
            this.nextElementSibling.classList.toggle("slideActive");
        });
    }
</script>

CSS:

.accordion {
    height: 0px;
    transition: 0.3s;
}
.accordion .slideActive{
    height: 100px;
}

(Or whatever styling you may prefer)


You can check if it's active , by using:

this.classList.contains('slideActive');

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