Continuous Scrolling of Div in jQuery

I have successfully implemented a jQuery slider that allows me to click on the next arrow and slide to the next image. However, I am now facing the challenge of making it scroll continuously. This means that when I reach the last image in the slider, the first image should scroll in from the right. To achieve this, I attempted to use jQuery append. While it is appending the element correctly, I am running into an issue where the position being printed is showing the old position instead of the appended position.


<div id="parent">
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
</div>

//jQuery
numberOfSlides = 4; //Total number of images
$('arrow').click(function(){
if(currentSlide == 2) 
{
   $('first-child').append('parent');
}
else
{
  for(var i = 0; i < numberOfSlides; i++)
  { 
    $('.child:nth-child('+i+')').animate({'marginLeft','xxpx'}); // Sliding all images to the left
  }
}
currentSlide++;
});

The correct output when currentSlide is at 2 should be -800, 0, 800, 1600 for slides 2, 3, 4, 1 respectively. In the code above, the if condition will append the first element to the parent element. However, when retrieving the position of the appended element, it is displaying as -1600px (based on Firebug inspection showing the div on the right), instead of the expected 1600px. I am using marginLeft for positioning.

Thank you!

Answer №1

Alternatively, you can duplicate your content element and position them vertically adjacent to each other, scrolling them simultaneously. The current scrolling functionality should remain intact, as the jump will be seamless from the bottom of one element to the top of the other, creating a continuous visual effect. To achieve this, place both copies of your content in a container and scroll that instead, simplifying the process by only having to move one element.

If you want to further enhance the performance, consider displaying only the top portion of the content in the bottom element to conceal the transition. However, unless your content is particularly intricate or resource-intensive, this additional optimization may not be necessary.

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

Strip away double quotation marks from object attributes except those beginning with a number

I've searched extensively and reviewed various Q&A forums, but I haven't encountered a scenario quite like this. Here's an example of the object I'm working with: props: { "label": "1rem", "text3": "1rem", "text2Button": "1re ...

How to access a global variable within an Angular application

Using node express and angular, I have set up a route in my express app.js where I pass a variable to the rendered page: app.get('/test', function(req, res){ res.render('test', { user: 12345 }); }); Within my 'test' view, ...

Invoking an asynchronous method of the superclass from within an asynchronous method in the subclass

I'm currently developing JavaScript code using ECMAScript 6 and I'm facing an issue with calling an asynchronous method from a superclass within a method of an extending class. Here is the scenario I'm dealing with: class SuperClass { c ...

Maintaining the aspect ratio of images in Bootstrap Carousel: A complete guide

Using the default carousel from Bootstrap template has been working well for me. However, I've encountered an issue where resizing the browser window distorts the image aspect ratio by squishing it horizontally. I've tried various solutions to m ...

Validating forms using Ajax, Jquery, and PHP, with a focus on addressing

I am currently working on processing a form using AJAX, jQuery, and PHP. However, I have encountered a 404 error while doing so. I tried searching for a solution before posting here but unfortunately couldn't find one. Below is the content of my .js f ...

Can a URL be utilized as a selector in web development?

Looking for some assistance with a coding task. I have a website called site.com and I need to verify if a specific classname is present on another page within the same domain. The page in question is located at . If the classname is indeed present, then I ...

Developing a TypeScript Library from Scratch

After following the instructions on this particular website, I have implemented the following JavaScript code: function A(id) { var about = { Version: "0.0.0.1", }; if (id) { if (window === this) { return new A(i ...

Tips on Avoiding Initial Click Activation

I've been working on a JavaScript function that dynamically generates an iframe with a button that, when clicked, deletes the iframe itself. Here are the functions I've written: function createIframe (iframeName, width, height) { var ifram ...

Trouble with getElementsByTagName function

Utilizing the NODE JS module, I have created an HTTP server that responds with a page containing JavaScript code to embed a webpage within an <iframe>. Within this <iframe>, I am trying to access its elements using the getElementsByTagName meth ...

How come my jQuery toggle is not functioning properly?

Can anyone help with my project? I'm having issues with my toggle function and I can't seem to figure out what's wrong. $('.col1-text').click(function() { $(this).find('.col1-info').toggle(function() { $(this ...

Using JavaScript to Align HTML Data to the Right or Left

I have successfully created an HTML table from JSON data and formatted it using JavaScript as required. However, I am facing a challenge with right-aligning all the amounts coming from my JSON data. I want all the numbers to be aligned to the right but I& ...

Mixing box-shadow and blur filters can result in unusual artifacts appearing in Webkit browsers

Recently, I encountered an intriguing and frustrating bug in WebKit browsers. Consider a scenario where there is a parent DIV (let's say .wrapper) and a child DIV (.main). You apply a box-shadow on the .main DIV and a blur filter on the .wrapper DIV. ...

unable to make a request to the express server with axios

I am in the process of developing a chat application similar to whatsapp. One of the key features I'm working on is that when a user clicks on another person's name, their chats will be displayed. However, currently, I'm facing an issue wher ...

Bootstrap is designed to dynamically adjust to the size of the browser window, ensuring a smooth

After completing the construction of a new website, I encountered an issue where the styles and layouts are responsive when adjusting the browser window size. However, upon opening responsive inspect tool, no changes occur - columns refuse to break and t ...

Go back to the pop-up window if an error occurs

I've implemented a modal window in front of my index page. When the model is valid, I redirect to the index view. However, when the model is not valid, I want it to stay on the modal. How can I achieve this? [HttpPost] [ValidateAntiForgeryToken] publ ...

Substitute the expression with placeholders for JavaScript variables

Looking for a JavaScript regex pattern that will replace all instances of {{variableName}} with the corresponding JavaScript variable placeholders in a given string, such as ${variableName}. let str = 'Lorem ipsum dolor {{variable1}}, sit amet consect ...

working with a list of Python objects in a JavaScript environment

Currently, I am building a web application using Flask and Python. In my Python code, I have a class that can be parsed as JSON. class uItem: itemCount = 0 def __init__(self, id, name): self.id = id self.name = name I am trying to acce ...

The bottom margin fails to function as expected

I've been trying to position a loading image at the bottom right of the page, but everything seems to be working except for the margin-bottom. <div id="preload"> <div align="right" style="margin-bottom:40px; margin-right:50px;"> ...

Showing php outputs within a container

I want to outline my goal in detail. I have a setup with 3 pages: index.html, form.html, and result.php. By using jQuery, when the button on index.html is clicked, form.html is loaded inside a div within index.html. Upon submitting form.html, it calls resu ...

Issue arising with data exchange between components using data service in Angular 5

Utilizing data service to share information between components has presented a challenge for me. References: Angular: Updating UI from child component to parent component Methods for Sharing Data Between Angular Components Despite attempting the logic o ...