Disable the moving of the DIV button with a left-margin of 0px

Having a bit of a challenge here. I'm attempting to craft my own slider using jQuery along with some css and javascript.

I've managed to get my slider functioning, moving a Div 660px to the left and right upon button clicks.

However, I'm hoping to disable the right button when the left margin reaches 0. Additionally, I'd like the entire div to reset back to 0px after a certain number of clicks.

Is this achievable?

Below is the code I'm currently using:

<script language="javascript">
            function example_animate(px) {
                        $('#slide').animate({
                            'marginLeft' : px
                });
            }
        </script>
        <div class="container">
            <div class="row">
                <div class="contr-left">
                    <a type="button" value="Move Left" onclick="example_animate('-=600px')"><i class="fa fa-chevron-circle-left fa-2x" aria-hidden="true" ></i></a>
                </div>
                <div id="carreview" class="carousel" data-ride="carousel" style="opacity: 1; display: block;">
                    <div class="wrapper-outer">
                        <div class="wrapper-inner" id="slide">
                            <?php get_template_part('loop-reviews');?>
                        </div>
                    </div>
                    <div class="contr-right">
                        <a type="button" value="Move Right" onclick="example_animate('+=600px')"><i class="fa fa-chevron-circle-right fa-2x" aria-hidden="true" ></i></a>
                    </div>
                </div>
            </div> 
        </div>

The code I'm utilizing can be found on this page: Page

After applying the code from Rayn, the button now hides, but unfortunately, it doesn't reappear when the left-margin is set to 1px. Hence, I am now attempting the following solution: (although it does not work)

Trying this now: (does not work though) `function example_animate(px) {

                $('#slide').animate({
                    'marginLeft' : px
        });
       var slidemarginleft = $('#slide').css('margin-left'); //this gets the value of margin-left
       if(slidemarginleft == '0px'){
          $('.contr-right').hide();
      } else (slidemarginleft == '1px') {
            $('.contr-right').show();
         }
       }`

Additionally, I've noticed that the margin-left property isn't defined in the style sheet but within the

<div style="margin-left:0px">content</div>

Answer №1

To ensure that the element has a margin of 0px after each click, you can incorporate an if statement in the code.

    function animated_example(px) {

                    $('#sliding').animate({
                        'marginLeft' : px
            });
           var slide_margin_left = $('#sliding').css('margin-left'); //retrieves the margin-left value
           if(slide_margin_left == '0px'){
              $('.right-control').hide();
             }
           }

Answer №2

Implement this

function animate_example(px) {
                    $('#slide').animate({
                        'marginRight' : px
            });
            if($('.xman').css("marginRight")=='0px'){
                $('.contr-left').attr('disabled',true);
           }
        }

Answer №3

Deactivate Button on Margin-Left: 0px

To prevent a button from being clicked when the margin-left property is set to 0px, you can utilize the .prop() method in jQuery. Here is a simple example in pseudo code:

function deactivateButton() {
 $(element).prop('disabled', true);
}
function checkMargin() {
if ($(element).css("margin-left") === "0px") {
 deactivateButton()
}
}
checkMargin()

Reset to 0px after a series of clicks

To reset the margin-left back to 0px after a certain number of clicks, you can track the number of clicks and trigger a function once the threshold is reached. Here is a pseudo code example:

var threshold = 5
var clicks = 0

$(element).click(function(){
clicks++
if (clicks === 5) {
resetMargin();
}
clicks - 5
})

function resetMargin() {
 $(element).css( "margin-left", "0" );
checkMargin()
}

Answer №4

After incorporating your suggestions, here is the updated code:

<script>
        function animateSlide(px) {

                    $('#slide').animate({
                        'marginLeft' : px
            });
           var slideMarginLeft = $('#slide').css('margin-left'); // getting the margin-left value
           if(slideMarginLeft < '-3000px'){
              $('#slide').animate({marginLeft: '0px'}, 400);
             } 
           var hide = $('#slide').css('margin-left'); // getting the margin left value
           if(hide >= '-50px'){
              $('.contr-left').addClass('showMe');
             }
           var hideMe = $('#slide').css('margin-left'); // getting the margin left value
           if(hideMe <= '-50px'){
              $('.contr-left').removeClass('showMe');
             }
           }
</script>
<!-- /Review script -->

<section id="reviews">
    <div class="container">
        <div class="row">
            <div class="container">
                <div class="row">
                    <h4>Reviews</h4>
                </div>
            </div>
        </div>
    </div>
    <div class="container">
            <div class="row">
                <div class="col-sm-1" style="width:40px;">
                    <div class="row">
                        <div class="contr-left">
                            <a type="button" value="Move Left" onclick="animateSlide('+=510px')"><i class="fa fa-chevron-circle-left fa-2x" aria-hidden="true" ></i></a>
                        </div>
                    </div>
                </div>
                <div class="col-sm-10">
                    <div class="row">
                        <div id="carreview" class="carousel" data-ride="carousel" style="opacity: 1; display: block;">
                            <div class="wrapper-outer">
                                <div class="wrapper-inner" id="slide" style="margin-left:0px;">
                                    <?php get_template_part('loop-reviews');?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-1" style="width:40px;">
                    <div class="row">
                        <div class="contr-right">
                            <a type="button" value="Move Right" onclick="animateSlide('-=510px')"><i class="fa fa-chevron-circle-right fa-2x" aria-hidden="true" ></i></a>
                        </div>
                    </div>
                </div>
            </div>
            <a href="<?php site_url(); ?>/reviews"><div class="btn btn-06">View all reviews</div></a>
        </div>
</section>

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

Surprising outcome when attempting to load a partial view into an Internet Explorer 8 DOM using jQuery Ajax

Encountering a peculiar issue while loading a Partial View with jQuery Ajax in IE8. The result appears distorted when viewed using this browser. The hypothetical Partial View causing trouble looks like this: <aside>Example test</aside> After ...

Retrieving various sets of JSON objects arrays

I have successfully stored an array of JSON objects in the database using the following code: function send_custom_markers() { var reponse = confirm("Send markers to selected contacts?"); if (reponse === true) { var json_markers = new ...

Navigating from a view to a controller in MVC to reach a different view: a step-by-step guide

I implement the MVC model using PHP, JS, HTML, and CSS. Additionally, I utilize CodeIgniter as my framework. I am looking to understand how to redirect to another controller. While on the Home page of my project, when clicking on "details" (div = prod_det ...

How to use JavaScript to retrieve extensive data streams (exceeding 1GB) from the internet

I'm interested in finding out if there is a way to stream data directly from JavaScript to the browser's download manager. With WebRTC, I am able to stream large amounts of data (files over 1GB) from one browser to another. On the receiving end, ...

Can we access an element within the document using its context?

One common method to access an element is by using its id: <div id="myId"></div> <script type="text/javascript"> $(document).ready(function(){ $('#myId').something(); }); </script> However, there are inst ...

What is the reason my bootstrap carousel isn't functioning properly?

My carousel is only displaying the first image and seems to be stuck. The navigation buttons are also not functioning. Here is the code for reference: <main> <div id="slider" class="carousel slide" data-mdb-ride="carousel"> <div cl ...

Creating a basic live data visualization chart

Can anyone help me with fetching data from the database and plotting it into a real-time graph? I found an example here: The JSON structure is as follows: "networks": { "eth0": { "rx_bytes": 5338, "rx_dropped": 0, "rx_err ...

Extract data from a website with Python and selenium

I need to scrape the data from a table that seems to be generated in JavaScript. I'm using selenium and Python3 for this task. While looking at how others have approached similar challenges, I noticed they use xpath to locate the tables before scrapin ...

In my React JS class component, I am looking to have the image display switch each time the button is clicked

How can I change the image when clicking a button in a class component? I am familiar with function components, but unsure how to achieve this. class CoffeeImages extends React.Component{ constructor(props){ super(props) ...

Using PHP regular expressions to find the final occurrence trailing after the last match in HTML

Looking to analyze a webpage (where the same product price is compared on different stores - each store has a block with its logo, price...) I want to separate each store into a different array element using preg_match_all I am trying to skip the advert ...

using percentage and float to enforce overflow-x

I have a small structure of list items inside a container and I am trying to display horizontal overflow instead of vertical overflow. However, I am having trouble achieving this, possibly due to the properties like float and percentage that I am using. I ...

Error in PromisifyAll: Callback parameter must be a function

Either my understanding of how BlueBird and its promisify function works is incorrect, or I am making a mistake in the following code. I have an "upload-handler" module that exports one function with a callback: The structure of the upload-handler functio ...

I'm utilizing bootstrap to design a slider, but the length of the second div inside it is longer than the slider div

https://i.sstatic.net/0iIPb.png To ensure that the second div appears above the slide div, I have applied the position:absolute property to the second div. Since the first div is a slide, I had to position the second div outside of the first div. Do you h ...

Javascript code for scrolling to the top isn't functioning as expected

I found a helpful code snippet for adding a scroll to top button on my webpage at the following link: http://jsfiddle.net/neeklamy/RpPEe/ Although I implemented the code provided, unfortunately, the scroll to top button is not displaying correctly. Here ...

Adding content to a text field and then moving to the next line

I am looking to add a string to a text area, followed by a new line. After conducting some research, here are the methods I have attempted so far but without success: function appendString(str){ document.getElementById('output').value += st ...

"Using Bootstrap's toast feature repositions every element on the

I am working on a website project and encountering an issue with a bootstrap toast. The toast currently appears in the top right corner, but I would like it to display above the carousel without affecting the layout. Instead, when the toast appears, it shi ...

An error occurred while trying to access the 'length' property of an undefined value in a code using ColdFusion and jQuery DataTable

I'm currently working on a ColdFusion page to generate a jQuery DataTable that retrieves JSON data. Even though I have set up the DataTable in jQuery and provided the correct URL for fetching the data, I keep encountering this error Uncaught TypeE ...

Problem with CSS layout on Internet Explorer 9

I've encountered some layout difficulties in IE9 and I'm struggling to identify the root of the problem. Even though I'm using Foundation 5, I don't believe that's the issue. The content is within a Foundation grid set at large-12 ...

Guide on generating a fresh array of objects that encompass distinct items, alongside their combined prices and quantities using JavaScript

I am struggling to generate a new array of objects from one that contains duplicates. For instance, here is the console.log output of the current array: console.log(items); [{ _id: 5eb2f7efb5b8fa62bcd7db94, workName: 'best item ever', ...

Hyperlinks functioning properly in Mozilla Firefox, yet failing to work in Internet Explorer

I'm encountering issues with my links not functioning properly in Internet Explorer. <a style="text-decoration:none;" href="<?php echo base_url();?>index.php/person/create/<?php echo $this->uri->segment(4);?>" > When I check ...