Establishing limits for manipulation through .animate in Jquery

I've created a basic slider using Jquery that includes left_arrow and right_arrow div elements. These divs control the 'left' value of my #screens div, which contains a long .png image. Each screen within the slider is 543px in width.

$pr('#arrow_left').click(function () {
    $pr('#screens').animate ({"left": "+=543px"}, "fast"); 
});
$pr('#arrow_right').click(function () {
    $pr('#screens').animate ({"left": "-=543px"}, "fast");
});

Although the sliding action works smoothly, I now want to establish minimum and maximum values for the 'left' property of #screens so that users cannot navigate beyond the boundaries of the image in either direction.

Answer №1

let maximum = 543 * 10; //10 slides in total
let currentSlide = 0;
$pr('#arrow_left').click(function () {
    if(currentSlide < 0)
    {
        currentSlide += 543;
        $pr('#screens').animate ({"left": currentSlide + "px"}, "fast"); });

    }
});

$pr('#arrow_right').click(function () {
    if(currentSlide > maximum)
    {
        currentSlide -= 543;
        $pr('#screens').animate ({"left": currentSlide + "px"}, "fast");

    }
});

Answer №2

I found Kai Qing's solution to be really useful :) I tweaked it a bit to fit my own requirements and included jQuery to dynamically adjust the max variable based on the number of direct child <div> elements in the slider. This way, there's no need to constantly modify the code once it's been configured :)

Just sharing the code snippet here in case anyone else finds it helpful :p

$(document).ready(function(){
var max = 275 * $('.slider-wrap > div').length; //replace div with the element that contains each content piece you're sliding.
var current = 0;
$('.left').click(function () {
if(current < max){
    alert(current);
    current += 275;
    $(".slider-wrap").animate({marginLeft:current + 'px'},'slow'); 
    }
});
$('.right').click(function () {
if(current > 0){
    alert(current);
    current -= 275;
    $(".slider-wrap").animate({marginLeft:current + 'px'},'slow'); 
    }
});

});

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 incorrect results when trying to identify the class of the clicked image

I am currently exploring how to identify the specific image that I have clicked on using jQuery. Although my current code is returning IMG as the output when I click on an image, this is not the desired result. My intention is to get the output of the clas ...

Using a jQuery AJAX request to update the quantity of available items based on user input

Is there a way to use jQuery to capture user input values and product value, then send it to the backend? I need this to trigger an AJAX call that will hit a Java backend. The goal is to provide a product number and quantity value in order to generate th ...

Can you explain the distinction between $(this) and $("#id") for me?

Within an $.each() loop, I've encountered a specific issue that I can't seem to understand why it's failing. For example, when I use an if statement like the one below: if(($(this).attr("some-attribute"))) It always returns false, regardl ...

The ID parameter is not being included in the AJAX request URL

I'm currently focused on working with categories and their subcategories in my code. I am encountering an issue where the id from the category is not being passed correctly into Ajax along with its URL. Here is a snippet of my code: <script type=" ...

Resizing Checkboxes in Internet Explorer 9

IE9 is causing checkboxes to render stretched, while all other browsers maintain the size of the checkbox but expand a clickable invisible area. Is it possible to disable this behavior in IE9 using CSS without affecting the behavior of other browsers (inv ...

When the browser width is reduced, the text appears outside of the image

When pasting text into an image in this example, everything looks fine at first. However, as the browser width is reduced, the text starts slipping out of the picture. To make matters worse, unsightly line breaks are also added. Is there a way to prevent ...

Enhancing the layout of an ASP.NET master page with an HTML table that extends beyond the

My ASP.NET (VB) master page contains a table with 9 columns, each intended to hold textboxes. However, even without textboxes, the cells are extending beyond the content margins. How can I adjust them to fit within the master page margins? If they still ...

Utilizing jQuery and CSS for positioning in my debut project: an Etch A Sketch

Hello! I am currently diving into the world of CSS, jQuery, and Javascript, and my first project was creating an etch-a-sketch. So far, I've implemented a few features that are working smoothly. However, I've encountered an issue with the snake f ...

Exploring the functionality of jQuery by incorporating a variable into the selector

I attempted to modify the src attribute of an image file using a variable, but it did not actually change. Can anyone pinpoint where I went wrong in using the variable? jquery var p2begen = "416"; $("[id=i'" + p2begen + "']").attr("src", "check ...

Guide to displaying options in the Vue material select box even when the default value is blank

I have implemented the material design framework vuematerial with vue.js. In traditional HTML, a selection box looks like this: <select> <option value="">You should initially see this</option> <option value="1">One</o ...

Sending a post request with Ajax in an ASP.NET Webforms application

When attempting to post data to the server using an AJAX call, I encountered an error. var userDetails = {}; userDetails["Name"] = "Saran"; var DTO = { 'userDetails': userDetails }; $.ajax({ type: "POST", contentTyp ...

Having trouble removing or adding a class to an HTML element?

I have a collection of three colored buttons. My goal is to allow users to select one button at a time, triggering it to be highlighted while the others are unselected. Each button corresponds to an object with a property called isSelected that can be set ...

How to prevent the scroll bar from affecting a fixed div located at the top of the page

Check out this website: I am struggling with setting up a fixed menu div at the top and a content div below it on my site. I want the scroll bar to only apply to the content div and not the header div at the top, but I can't seem to figure out how to ...

Struggling to align form inputs next to each other

I have tried various methods such as adjusting padding and margins, using tables, displaying as inline and inline-block, and separating parts of the code into different divs. However, I am still unable to get my inputs to be displayed side by side. Here is ...

A guide to troubleshoot and resolve the 500 (Internal Server Error) encountered during an Ajax post request in Laravel

I am facing an issue in my project where I am trying to store data into a database using ajax. However, when I submit the post request, I get an error (500 Internal Server Error). I have tried searching on Google multiple times but the issue remains unreso ...

Trying to prevent AJAX from running on the undo link

I am currently working on implementing a feature that allows users to delete a list with an undo operation. When a user deletes the list, they are redirected from the list detail page to the main lists page where there will be an "undo" link available for ...

Troubleshooting problems with background-image in CSS using Javascript

My latest project involves coding to assign background images to various classes using jQuery. The image files are named in a numerical order, such as 0bg.jpg, 1bg.jpg, 2bg.jpg, and so on. for (i=0; i < 8; i++) { $('.hpCarousel:eq('+i+' ...

Unable to apply 3rd condition with ngClass into effect

I've been attempting to incorporate a third condition into my ngClass. Initially, I managed to get two classes working in my ngClass to change the row colors alternately. [ngClass]="{ totalrow:i%2 != 0, odd:i%2 == 0}" Now, I'm trying to introdu ...

Prevent jQuery mouseover from hiding an element when the mouse is hovering over a popup window

I am working on a project where I need popup windows to appear when the mouse hovers over certain elements for a few seconds. I have managed to achieve this using the script below: var timeOutUserInfo; $('a[datatype=popupAboutUser]').live({ ...

When the content in one box exceeds the content in the other, one box is considered to be overfilled

For the image, click here: I am facing an issue with boxes stacking on top of each other. When one box has more content, it overlaps the other. CSS: #destaques_container{ margin: 0 auto; } #destaques_container_dentro{ float: left; margi ...