What techniques can be used to avoid blinking while forcefully scrolling to the left?

In my previous inquiry about dynamically adding and removing divs on scroll, I was unable to find a satisfactory solution among the responses provided. However, I decided to take matters into my own hands and attempted to implement it myself.

My approach involved detaching the previous div tag from the container and appending a new div tag when the scroll position reached between 500 and 600. Despite successfully achieving this functionality, I encountered an issue with the scrollLeft property of the container. After deleting and appending blocks, the left position would change, causing a blinking effect when forcibly scrolling to the left at a set point of scrollleft:100.

How can I prevent this blinking effect when scrolling to the left side forcefully? Here is a link to the jsfiddle for reference.

Answer №1

It seems like you are interested in implementing an infinite horizontal scroll with lazy-loaded images based on your question. If you want to achieve this functionality without removing images, you can dynamically adjust the width of the scroll container and handle the scroll position accordingly.

To demonstrate, here is a code snippet:

  $(function() {

    var scrollContainer = $("#container");
    var scrollContents = $("#scroll-contents");
    var colors = ['rgba(143, 146, 199, 0.49)', 'rgba(199, 143, 186, 0.49)', 'rgba(149, 199, 143, 0.49)', 'rgba(229, 86, 61, 0.49)',
      // Additional color values...
    ];

    function addBlock(blockNum) {
      scrollContents.append('<div class="block block' + blockNum + '" style="background:' + colors[blockNum] + '"><h4>' + (blockNum + 1) + '</h4></div>');
      document.getElementById('cont').innerHTML = blockNum + 1;
    }

    var totalBlocks = colors.length;
    var numBlocks = 6;

    scrollContents.width(totalBlocks * 400);

    for (var iBlock = 0; iBlock < numBlocks; iBlock++) {
      addBlock(iBlock);
    }

    scrollContainer.scroll(function () {
        if (numBlocks !== totalBlocks && (scrollContainer[0].scrollLeft > (numBlocks - 5) * 400)) {
            addBlock(++numBlocks);
        }
    });

  });
#container {
  width: 100%;
  height: 350px;
  position: relative;
  overflow: scroll;
  background: white;
  top: 50px;
  overflow: hidden;
}
.block {
  display: inline-block;
  width: 400px;
  height: 350px;
}
#scroll-contents {
  height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/asvd/dragscroll/master/dragscroll.js"></script>

<body>
  <div id="container" class="dragscroll">
    <div id="scroll-contents">
    </div>
  </div>
  <div id="cont" style="margin-top:150px; background:blue; height:20px; width:50px; color:white;">
  </div>
</body>

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

Receiving the error "Undefined chart type" when using $q.all with Google Chart API Promise

I am currently working on implementing angular-google-charts using the $http service to retrieve data and display it on charts. I found a helpful tutorial on this blog post: since the GitHub README for angular-google-charts lacks examples of backend data ...

Aligning a child div within a parent div using HTML/CSS where the child div takes up 30% of the parent div and

Can anyone help me figure out why these two INPUT elements aren't aligning next to each other? I appreciate any assistance you can offer! <STYLE> html, body { width: 100%; height: 100%; margin: 0px; padding: 0px; } </STYLE& ...

Error: The function app.all is not defined

This is a basic Angular page that I have set up. <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> ...

The unique identifier for a specific child element in Firefox

While working on this website: I customized the right sidebar navigation using: .sidenav a:after { display: block; position: relative; z-index: 0; top: -3.6em; left: 0.15em; width: 90%; content: "."; font-family: 'R ...

Using ui-grid to show cells as drop-down menus

I have a ui-grid example where one of the columns represents gender ('Gender': male, female...). The json data binding to the grid only contains code types like (1, 2, 3...). However, I want to display the gender name such as 'male' if ...

Overlay with a progress bar that obscures visibility

I'm dealing with a progress bar that needs to be displayed on top of an overlay. If you take a look at this jsfiddle, you can see the issue. Here's the markup: <div class="overlay mouse-events-off"> <div class="progress progress-st ...

Using string replacement for effective search finding: Unleashing the power of substring matching

I have a method that adds an anchor tag for each instance of @something. The anchor tag links to a specific sub URL. Check out the code: private createAnchors(text: string) { return text.replace(/(@[^ @]+)/ig, '<a href="/home/user/$1">$1& ...

Import objects into THREE.js using OBJLoader, convert to Geometry, apply transformations, and finally convert to BufferGeometry

Trying to figure out why the geometry loaded with OBJLoader cannot be smoothly shaded. var loader = new THREE.OBJLoader(manager); loader.load('/manmodel/js/man.obj', function (object, materials) { console.log(object); console.log(materia ...

Tips for sequentially arranging and rearranging an array of numbers, even when duplicates are present

Encountered a perplexing issue that has me scratching my head in an attempt to visualize a solution. Currently, I am working with an array of objects that appears as follows: let approvers = [{order:1, dueDate: someDate},{order:2, dueDate: someDate}, ...

How come JSON.parse is altering the data within nested arrays?

In my journey to master Angular 2, I decided to challenge myself by creating a Connect Four game using Angular CLI back when it was still utilizing SystemJS. Now, with the switch to the new Webpack-based CLI, I am encountering a peculiar issue... The fun ...

Having issues when dynamically adding options to a multiselect dropdown

I'm working on dynamically adding options to a multiselect using AJAX: <select size='2' name="CraftCode" id=@ccrf class="form-control js-select manualentrydd" ></select> $.ajax({ type: "GET", url: "GetCraftCodes", data: ...

Using PHP along with JQuery and AJAX to update the database when a Div element is hidden

My website has a complex setup, so I will simplify it with an example. Current Configuration I currently have two buttons. <a id="one" href="#">Link 1</a> <a id="two" href="#">Link 2</a> Additionally, I have two divs. <div i ...

An effective approach to automatically close an Expansion Panel in an Angular Mat when another one is opened

I am attempting to implement functionality where one expansion panel closes when another is opened. By default, the multi attribute is set to "false" and it works perfectly when using multiple expansion panels within an accordion. However, in this case, I ...

Transmitting keys and data from Sails.js to Angular via streaming

In my current setup, I am using an angular frontend to fetch data from a sails backend through the sails-mysql adapter and display it in a ui-grid. However, due to the large size of the dataset, there is a noticeable delay in loading the data before the pa ...

Unable to change the background color of h1 tag in a ReactJS project

import React from 'react' export default function Home(){ return<div> <h1 className="bg-dark text-white p-3">Home Page</h1> </div> } I am attempting to modify the background color of the h1 tag in my Reac ...

Issue with NodeJS: The HTTP GET request is returning the code instead of the expected JSON object

My NodeJS code is designed to fetch a JSON object from a website. Here is the snippet: var http = require('http'); var url = { host: 'www.sample-website.com', headers: { "Accept": "application/json", 'Content-Type&apos ...

What is the best way to show HTML code on a REACT website without disrupting the existing styles?

I am trying to showcase the following code in a REACT webpage, but it is not displaying as code. Instead, it is showing as actual elements. How can I display the button codes below as actual code? <code> <pre> <Button className='btn-grv ...

The rotation transformation on the Z-axis occurs once the animation has finished

Currently, the rotation on the Z-axis is occurring after the animation, even though I am still in the early stages of creating an animated hand opening. The intention is for the fingers to only rotate on the X-axis to simulate a realistic hand opening moti ...

Troubleshooting Key Press Issues with Bootstrap 5 Dropdown and Collapse Feature

Exploration In my quest to create a dynamic Bootstrap (5) Navigation bar/menu with animated sub-menus, I stumbled upon a solution that seems to fit the bill perfectly. By employing data-bs-toggle="collapse" instead of 'dropdown', I dis ...

Retrieving values of checked boxes in a table using JavaScript

While using the select method, I had this line of code: Person_name = $('#selectedPerson').val() When selecting 2 values, person_name.length displays as 2. Recently, I switched to checkboxes displayed in a table. However, when I use person_name ...