Restricting the number of dots on a slick-slider without using jQuery

I am facing an issue where the dots in my slider keep adding up and forming multiple lines when there are more than 30-40 images. I aim to achieve a dot navigation similar to Instagram, where clicking on a dot displays the corresponding image with the active dot sliding into the middle.

Although slick-slider allows for selecting slidesToScroll to group the images (instead of scrolling one by one), this does not effectively address the problem on mobile devices.

Since jQuery is not included in the project, I am looking for JavaScript or JSX solutions to implement this functionality.

I want something like what is shown in this example:

https://codepen.io/nazarkomar/pen/RdRjqJ

$(document).ready(function() {

    var slider = $('.main-slider');

    slider.slick({
        dots: true
    });

    function loadSliderDotClasses(stickSlider) {

        var dot = stickSlider.find('.slick-dots li.slick-active'),
        dotSize1 = 'dot-size-1',
        dotSize2 = 'dot-size-2',
        dotSize3 = 'dot-size-3';

        stickSlider.find('.slick-dots li').each(function() {
            $(this).removeClass(dotSize1).removeClass(dotSize2).removeClass(dotSize3);
        });

        dot.prev().prev().prev().addClass(dotSize1);
        dot.prev().prev().addClass(dotSize2);
        dot.prev().addClass(dotSize3);
        dot.next().addClass(dotSize3);
        dot.next().next().addClass(dotSize2);
        dot.next().next().next().addClass(dotSize1);
    }

    loadSliderDotClasses(slider);

    slider.find('.slick-dots li').on('click', function() {
        loadSliderDotClasses(slider);
    });

    slider.on('swipe', function(event, slick, direction){
        loadSliderDotClasses(slider);
    });
});

https://codepen.io/nazarkomar/pen/RdRjqJ

Is it possible to convert this to JavaScript?

Answer №1

Here is a simple solution that uses vanilla JavaScript instead of jQuery. With just a few replacements, you can get started without spending too much time on it.

  1. Instead of using $(element).remove() and $(element).add(), try using the classList alternative.
  2. To find the index of the .slick-active node, use
    Array.from(element.parentNode.children).indexOf(element)
    .
  3. You can loop through the .slick-dots li element array to selectively apply classes based on their relation to the active dot's index.

Answer №2

If you want to reduce the number of visible dots on your screen, one effective method is to make use of the `scrollIntoView` JavaScript function within a slick carousel. By adding an `afterChange` event handler to the carousel, you can achieve this functionality.

var slider = $('.slider').slick(options);

slider.on('afterChange', function(event, slick, currentSlide) {
  var activeDot = $(event.target).find('.slick-dots').find('li.slick-active');
  activeDot.get(0).scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' });
});

Check it out on codepen: https://codepen.io/ahmu83/pen/vYbaRzQ

Another alternative solution using CSS can be found here: https://github.com/kenwheeler/slick/issues/1799#issuecomment-147077244

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having difficulty accessing and updating data in a database while using Discord.js

My goal is to enable staff to respond to the last direct message (DM) by using ~reply <message> instead of ~dm <userID> <message>. However, before I can do that, I need to store the user's ID in a database to identify the last person ...

Using CSS to Create Text Wrapping

I have a massive string of randomly generated numbers that I am trying to display in a div block. Since the string is quite long, it's currently being shown in one single line. For instance: String str="13,7,5,1,10,7,18,11,17,10,9,16,17,9,6,19,6,13, ...

locate sibling element

<div class="videoItem"> <div class="innerVideoItem"> <a><div class="overlayBg"></div></a> <a><img class="overlayPlay"><img></a> </div> </div> <script> ...

CSS: Set the left position to 250px with the added feature of hiding any overflow

I am currently working on a sidebar that can be shown or hidden using JavaScript with CSS animations. To achieve this effect, I need to position the content div (#main) using left: 250px;. #main { left: 250px; position: relative; overflow: hidden; } ...

Looking to enlarge a few CSS classes, but unsure of the best approach

My app has some CSS code that I found, but it's too small. Can anyone help me increase the size by 150%? I am using React-Bootstrap for styling, so I'm looking for a way to enlarge the element without adjusting every attribute in the CSS classes. ...

Ways to adjust the color of a navbar link according to the current page you are visiting?

I'm looking to implement a feature on my website where the area around the nav-link darkens when someone navigates to a certain page, making it easier for them to see which page they are currently on. I am working with Bootstrap v4.3.1 and Razor Pages ...

Unable to render Blender model in threeJS scene due to undefined condition

I've tried following solutions from Stack Overflow to resolve this issue, but I'm still unable to load external objects (Blender). Essentially, I exported it as a ThreeJS JSON file, here is the content of my JSON file: { "textures":[], ...

Is there a way to customize the appearance of the current slide in slick.js with jQuery?

I am looking to customize the appearance of the currently active slide by giving it an orange border. Keep in mind that the class for the active slide is : slick-active and the structure is as follows : <div class='slick-slide slick-active' ...

I must determine whether the contents of an array exceed zero

THE SUMMARY: I have three value numbers for an array. If the total addition of the array's elements is greater than 0, I need to display "el funcionamiento no es infinito", otherwise "es infinito". It seems that it's not working because I belie ...

What is the process of installing an npm module from a local directory?

I recently downloaded a package from Github at the following link: list.fuzzysearch.js. After unzipping it to a folder, I proceeded to install it in my project directory using the command: npm install Path/to/LocalFolder/list.fuzzysearch.js-master -S Howe ...

What is the reason for the inability to run php code within html files?

Struggling with a stubborn issue that has left me feeling stuck for days. I'm attempting to execute a php script, involving mysql database access, from an html file using javascript. However, each time I run the code in NetBeans, it throws an unspecif ...

The compatibility between V-model and TinyMCE is unreliable

I've been working on integrating Vuejs and TinyMCE, using the @tinymce/tinymce-vue package for the Vue integration. I followed all the instructions and everything seems to be functioning properly - I can write, insert images... everything except for t ...

"React issue: Troubleshooting problems with using .map method on arrays of

I am struggling with the following code: const displayData = (data) => { console.log(data);// this displays an array of objects if (!data.length) return null; return data.map((movie, index)=>{ <div key={index} className=&qu ...

Modify URL parameters using history.pushState()

Utilizing history.pushState, I am adding several parameters to the current page URL after performing an AJAX request. Now, based on user interaction on the same page, I need to update the URL once again with either the same set of parameters or additional ...

Trouble with top attribute functionality within animate function

Why does the top attribute in the animate function of JQuery not seem to work, while the opacity attribute functions correctly in the code snippet below? $(function() { $(window).on('scroll', function() { ...

Disabling font-awesome checkboxes

As someone who is new to front-end technologies, I am trying to achieve the following. Despite my efforts to find a solution, I have not been successful so far. Here is the issue I am facing-- I have two master checkboxes labeled 1) I do not like numbers ...

Updating the ghost image for HTML5 drag and drop functionality

I am facing an issue with a large div element that is too big for my liking. I have come up with a solution to change it by implementing the following code: <div draggable = "true" id = "ololo"> </div> var k = document.getElementById("ololo"); ...

Tips for aligning website content (including the body and navigation bar) at the center in rtd sphinx

Trying to create documentation with the Read the Docs theme for Sphinx documentation. Want to center the entire webpage, including both the body and left navigation bar so that as the window width increases, the margins on both sides increase equally. Righ ...

Tips for creating an empty column in each succeeding row of a grid layout

Is there a way to create a grid layout with divs that leave space on the left side of each subsequent row? For example, having 4 div boxes in the first row, 3 in the second, 2 in the third, and so on. Demo https://i.sstatic.net/Gli6L.png Styling in CSS ...

Upgrade to V2.0: Substituting empty values with a specified string within a JSON formatted array

After submitting a form and receiving null values through JSON, I wanted to replace them with String values like 'n/a' or 'not specified'. Even though I followed @Winter Soldier's suggestion and added code to check for null values ...