2

I´m using slick.js slider in my angularJs web app. The plugin is working fine. I have the prev and next pagination buttons. All slick code is inside an anchor link to another page. When, I click in the prev or next button, the slick is sliding but the link is opened as well. So, I would like to prevent to open the link on buttons click that are styled over the link.

Please see simplified structure.

<a ng-href="wherever" target="_blank">          
        <div class="photo-box">                         
              <div class="slick-container" slick>
                    <div class="item" ng-repeat="photo in item.photos">
                        <img ng-src="url-to-image" />                   
                    </div>
                </div>
        </div>
<a/>

When the code is parsed, the buttons re created like: <button class="slick-next slick-arrow" aria-label="Next" type="button" style="display: inline-block;">Next</button>

As said, when I click in the button, the slider works but the link is opened as well.

Rober
  • 5,868
  • 17
  • 58
  • 110

1 Answers1

1

You might want to listen for click events on the <a> element and check if event.target.classList.contains('slick-arrow') is true and, in that case, call event.preventDefault() to prevent the link from opening.

In your template:

<a ng-href="wherever" target="_blank" (click)="handleLinkClicked($event)">          
  <div class="photo-box">                         
    <div class="slick-container" slick>
      <div class="item" ng-repeat="photo in item.photos">
        <img ng-src="url-to-image" />                   
      </div>
    </div>
  </div>
<a/>

In your component:

handleLinkClicked(e: ClickEvent) {
    if (e.target.classList.contains('slick-arrow')) e.preventDefault();
}

Otherwise, you could also try moving the <a> inside slick, maybe replacing the <div class="item" ...> if that works for you:

<div class="photo-box">                         
  <div class="slick-container" slick>
    <a class="item" ng-href="wherever" target="_blank" ng-repeat="photo in item.photos">
      <img ng-src="url-to-image" />                   
    </a>
  </div>
</div>
Danziger
  • 19,628
  • 4
  • 53
  • 83