Struggling to execute the horizontal scrollbar plugin

I came across a fantastic color swatch plugin that I'm using, you can find it here. This plugin defines a custom Vue element which is pretty neat.

In addition, I followed this example to implement a JavaScript-based scrollbar for my project.

Here's the content of my .vue file:

<script> element:

export default {
  name: "ProjectTask",
  data: function() {
     return { } 
 },
 methods:
    .
    .
    .
    .
  ,
  mounted() {
    const rightBtn = document.querySelector('#right-button');
    const leftBtn = document.querySelector('#left-button');

    rightBtn.addEventListener("click", function(event) {
       const content = document.querySelector('#content');
       content.scrollLeft += 300;
       event.preventDefault();
    });

    leftBtn.addEventListener("click", function(event) {
      const content = document.querySelector('#content');
      content.scrollLeft -= 300;
      event.preventDefault();
    });
  }
}

<template> element:

<div class="leftScroll" >
     <button id="left-button"> swipe left </button>
  </div>  
  <div class="ColorsWraper2 col-lg-8" id="content">
          <swatches v-model="Project.color" :colors="color_picker" shapes="circles" inline></swatches>
  </div>
  <div class="rightScroll">
     <button id="right-button"> swipe right </button>
  </div>

<style> element:

.ColorsWraper2 {
  white-space: nowrap;
  overflow-x: hidden;
  overflow-y: hidden;
}

The plugin's components should be positioned between the two buttons (left/right).

Unfortunately, I'm facing issues as it doesn't seem to work as expected!

Answer №1

If you're looking to center the color swatches between the buttons, I've achieved this using CSS Flexbox (CSS Flexbox Support).

To understand the steps I took and why, follow the detailed guide below. Otherwise, you can skip ahead to the code examples section for a brief overview of what was done.


Step 1: Enable CSS Flexbox

  • I enclosed the entire slider within a div called .scroll-slider, then set it to use flex with display: flex and flex-direction: row.
  • An important step was using align-items: center to vertically align the items (especially the buttons) and prevent them from expanding vertically.

Step 2: Prevent Overflow and Wrapping on Color Swatches

This step involved two parts:

  1. Remove overflow on the external wrapper .colors-wrapper using overflow: hidden;. Adding margin and padding to this wrapper ensures more accurate resizing.

  2. Prevent overflow on the inner wrapper, .colors-wrapper-inner, by setting white-space: nowrap; and overflow: hidden;.

Step 3: Fix Button Sizing

  • If you noticed the buttons getting squished, the simple fix is disabling flex-shrink with flex-shrink: 0.

Code Examples

JSFiddle: JSFiddle Link

StackOverflow Snippet:

// VueJS component setup
Vue.component('swatches', VueSwatches.default);

new Vue({
  name: "ProjectTask",
  el: "#app",
  data() {
    return {
      color: "#1CA085"
    }
  },
  methods: {

    swipeRight: function() {
      const content = document.querySelector('#content');
      content.scrollLeft += 300;
    },

    swipeLeft: function() {
      const content = document.querySelector('#content');
      content.scrollLeft -= 300;
    }

  },
  mounted() {
    const rightBtn = document.querySelector('#right-button');
    const leftBtn = document.querySelector('#left-button');
  }
})
/* General Styles */

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

/* Wrapper styling */

.scroll-slider {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.colors-wrapper {
  overflow: hidden;
  margin: 0 10px;
}

.colors-wrapper-inner {
  white-space: nowrap;
  overflow: hidden;
}

.scroll-button-wrapper {
  flex-shrink: 0;
}
<!-- Imports -->
<link href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85f3f0e0a8f6f2e4f1e6ede0f6c5b4abb5abb7">[email protected]</a>/dist/vue-swatches.min.css" rel="stylesheet" />
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aadcdfcf87d9ddcbdec9c2cfd9ea9b849a8498">[email protected]</a>/dist/vue-swatches.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ceae9f9dcaeb2a9b2adab">[email protected]</a>/dist/vue.js"></script>

<!-- HTML code within your template -->
<div id="app">
  <div class="scroll-slider">

    <div class="scroll-button-wrapper">
      <button class="scroll-button" id="left-button" @click="swipeLeft"> swipe left </button>
    </div>

    <div class="colors-wrapper">

      <div class="colors-wrapper-inner" id="content">
        <swatches v-model="color" shapes="circles" inline></swatches>
      </div>

    </div>

    <div class="scroll-button-wrapper">
      <button class="scroll-button" id="right-button" @click="swipeRight"> swipe right </button>
    </div>

  </div>
</div>

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

One way to determine whether .ajax is using Get or POST is to check the type parameter

I have a query: $.ajax({ url: "http://twitter.com/status/user_timeline/treason.json?count=10&callback=?", success: function (data, textStatus, jqXHR) { }, error: function (jqXHR, textStatus, errorThrown ...

Unexpected outcomes arise when parsing headers from a file-based stream

Currently, I am in the process of creating a small parser to analyze some log files using node streams (more specifically io.js). I have been referring to the documentation for unshift to extract the header. While I can successfully divide the buffer and ...

Using Angular 2: Exploring the power of observables for broadcasting events during a forEach loop

Upon testing the service within a forEach loop, I noticed that the parameter I passed to the service ended up being the last one in the iteration. I initially suspected that the issue was due to closures, so I attempted using an anonymous function to add ...

Navigating through sibling elements can be accomplished by using various methods in

Can someone help me figure out how to assign unique IDs to 6 different Div elements as I step through them? The code snippet below is not working as expected, giving all Divs the same ID. What is the correct way to accomplish this task? $('#main-slid ...

I am able to successfully make a REST API call using curl from the terminal, but when trying to access the same endpoint from my Vue application

If I run a curl command like this: curl --request POST \ --url http://localhost:3600/users \ --header 'content-type: application/json' \ --header 'user-agent: vscode-restclient' \ --data '{"email&qu ...

What is the process of changing the name of an object's key in JavaScript/Angular?

In my possession is an established entity, this.data = { "part": "aircraft", "subid": "wing", "information.data.keyword": "test", "fuel.keyword": "lt(6)" } My objective is to scrutinize each key and if the key includes .keyword, then eliminat ...

Is it possible to send a Word file as an email attachment using PHP's mail() function

When I sent a Word document file via email, it generated an unknown format like asdfADFDF 0000sadfas15454454. The mail function was used to send the email. Below is the code for that. Please review and let me know: function sendfile() { $id = $_REQU ...

Determining surface volume: Duplicate vertices found within the position array of the buffer geometry

I am currently working on calculating the surface area of a 3D model imported through the IFC Loader tool. However, before diving into the complex models, I decided to start with simpler shapes like pyramids to better understand the process. From my under ...

The behavior of React Router varies between local development on localhost and deployment on Heroku

Currently in the process of constructing a React-based website, initiated with create-react-app and incorporating react-router-dom`. The setup is as follows: index.js import React from 'react'; import ReactDOM from 'react-dom'; impor ...

Modifying button attribute through dropdown selection

In my project, I have a dropdown that gets its data from a database. Depending on the selection made in the dropdown, I would like to change the attribute of a button (data-uk-modal="{target:'#modal-'value of dropdown'}"). <select id "ci ...

Harnessing the power of JavaScript functions to display an image when clicked

Looking for help with making an image appear when clicking on one of three images? Despite trying different approaches, the desired result is still not achieved. I'm aware of using if else statements but exploring other methods. Any insights on what m ...

Shifted picture next to the accompanying words

I have successfully created a slideshow of images using JavaScript. <html> <head> <script language="JavaScript"> var i = 0; var path = new Array(); path[0] = "one.jpg"; path[1] = "two.jpg"; function swapImage() { document.slide ...

What causes the variable to be invisible in the imported file?

Within the main.js file, there is a specific code snippet: var test_mysql = require('./test_mysql.js') ... //additional code before(function(){ test_mysql.preparingDB(test_mysql.SQL_query.clear_data); // or test_mysql.preparingDB(SQL ...

What could be causing html-webpack-inline-source-plugin to prevent the page from refreshing after editing CSS files?

Currently, I am utilizing a plugin that embeds all CSS within the final HTML file. This method prevents the application from refreshing in development mode. Whenever I make edits to the CSS, the build process completes normally, but the styles do not updat ...

Adding Material-UI icons dynamically in a React TypeScript project requires understanding the integration of imported icons

I have a collection of menu buttons with icons, stored in an array of objects. The icon names are saved as strings that correspond to Material UI icons: interface MenuButton { text: string, onClickFunction: Function icon: string } export defau ...

Tips on adding background images to your chart's background

Is there a way to set an image as the background in Lightning Chart using chartXY.setChartBackground? ...

The server is unable to process the .get() request for the file rendering

Struggling with basic Express routing here. I want the server to render 'layout2.hbs' when accessing '/1', but all I'm getting is a 304 in the console. Here's the error message: GET / 304 30.902 ms - - GET /style.css 304 3.3 ...

Converting a string into an object using JavaScript

How can I transform the following string into an object? {src:'img/testimage.jpg', coord : {x:17, y:39}, width:200, height, 200} Update: I have a PHP file that generates JSON output. I used AJAX to retrieve the JSON in my JavaScript. By using ...

Smooth transitions between points in animations using the D3 easing function

I'm working on an animated line chart that displays data related to play activities. I've been experimenting with changing the animation style between data points on the chart using d3.ease Currently, my code looks like this: path .attr("stro ...

Tips for organizing an array of objects that contain null properties

Here is an array that I need help with: "data": { "risks": [ { "id": "22", "name": true, "surname": 0.5, "age": 0.75, "heigth" ...