Search code examples
amp-html

amp-carousel galleries active thumbnail


In the basic example of amp-carousel (Image Galleries with Previews) is there a way to change style of the active button? so the focused button (even in autoplay mode) is highlighted by a css style

<amp-carousel id="carousel-with-preview"  width="400" height="300" layout="responsive" type="slides" autoplay>
<amp-img src="https://unsplash.it/400/300?image=10" width="400" height="300" layout="responsive" alt="a sample image"></amp-img>
<amp-img src="https://unsplash.it/400/300?image=11" width="400" height="300" layout="responsive" alt="a sample image"></amp-img>
<amp-img src="https://unsplash.it/400/300?image=12" width="400" height="300" layout="responsive" alt="a sample image"></amp-img>
<amp-img src="https://unsplash.it/400/300?image=13" width="400" height="300" layout="responsive" alt="a sample image"></amp-img>
</amp-carousel>
<div class="carousel-preview">
<button on="tap:carousel-with-preview.goToSlide(index=0)"><amp-img src="https://unsplash.it/60/40?image=10" width="60" height="40" layout="responsive" alt="a sample image"></amp-img></button>
<button on="tap:carousel-with-preview.goToSlide(index=1)"><amp-img src="https://unsplash.it/60/40?image=11" width="60" height="40" layout="responsive" alt="a sample image"></amp-img></button>
<button on="tap:carousel-with-preview.goToSlide(index=2)"><amp-img src="https://unsplash.it/60/40?image=12" width="60" height="40" layout="responsive" alt="a sample image"></amp-img></button>
<button on="tap:carousel-with-preview.goToSlide(index=3)"><amp-img src="https://unsplash.it/60/40?image=13" width="60" height="40" layout="responsive" alt="a sample image"></amp-img></button>
</div>

Solution

  • although it took me hours searching for a way before posting, I found the solution right after :)

    this can be done using amp-bind, add amp-bind to head

    <script async custom-element=\"amp-bind\" src=\"https://cdn.ampproject.org/v0/amp-bind-0.1.js\"></script>
    

    add this before carousel

    <amp-state id="selected"><script type="application/json"> {"slide": 0} </script></amp-state>
    

    and this to the amp-carousel code

    on="slideChange:AMP.setState({selected: {slide: event.index}})"
    

    add class to button with active class if selected

    <div class="carousel-preview">
    <button [class]="selected.slide == 0 ? 'active' : ''" class="active" on="tap:carousel-with-preview.goToSlide(index=0)">title1</button>
    <button [class]="selected.slide == 1 ? 'active' : ''" on="tap:carousel-with-preview.goToSlide(index=1)">title2</button>
    <button [class]="selected.slide == 2 ? 'active' : ''" on="tap:carousel-with-preview.goToSlide(index=2)">title3</button>
    <button [class]="selected.slide == 3 ? 'active' : ''" on="tap:carousel-with-preview.goToSlide(index=3)">title4</button>
    <button [class]="selected.slide == 4 ? 'active' : ''" on="tap:carousel-with-preview.goToSlide(index=4)">title5</button>
    </div>