Exploring the Features of Bottom and Right in jQuery

Here is a snippet of jQuery code that I included in a sample program:

    $('#left').click(function(){
        $('.box').animate({
            left: "-=40px",
        }, function(){/* End of Animation*/});  
    });

    $('#right').click(function(){
        $('.box').animate({
            right: "-=40px",
        }, function(){ /* End of Animation*/}); 
    });

    $('#up').click(function(){
        $('.box').animate({
            top: "-=40px",
        }, function(){/* End of Animation*/});  
    });

    $('#down').click(function(){
        $('.box').animate({
            bottom: "-=40px",
        }, function(){ /* End of Animation*/}); 
    });

This is the HTML for the program:

    <div class="box">a box</div>

    <div id="navArrows">
        <button id="left" class="navigationArrow">&larr;</button>
        <button id="up" class="navigationArrow">&uarr;</button>
        <button id="right" class="navigationArrow">&rarr;</button>
        <button id="down" class="navigationArrow">&darr;</button>
    </div>

And here is the accompanying CSS:

#navArrows {
    position: relative;
    width: 150px;
    height: 150px;
    margin: 100px auto 0;
    background: #333;
    -webkit-border-radius: 100px;
    -moz-border-radius: 100px;
    border-radius: 100px;
    padding: 20px;
}

.navigationArrow {
    width: 50px;
    height: 50px;
    line-height: 50px;
    padding: 0;
    margin: 0;
    position: absolute;
    top: 20px;
    left: 20px;
    background: white;
    color: #222;
}

#up {
    left: 50%;
    margin-left: -25px;
}

#left, #right {
    top: 50%;
    margin-top: -25px;
}

#right {
    right: 20px;
    left: inherit;
}

#down {
    bottom: 20px;
    top: inherit;
    left: 50%;
    margin-left: -25px;
}

.box {
    height: 100px;
    width: 100px;
    background: #a7f;
    color: white;
    border: solid 4px #a1f;
    line-height: 100px;
    margin: 100px auto 0;
    opacity: 0.5;        
    position: relative;            
}

The purpose of this code is to move the div.box based on which button is clicked. The logic seems straightforward using the appropriate positions for each button click, but there may be some confusion with the code.

However, it works as intended when using the following values:
For Right and Left Buttons

{left: "-=40px"} or {left: "+=40px"} or {right: "-=40px"} or {right: "+=40px"}


For Up and Down Buttons

{top: "-=40px"} or {top: "+=40px"} or {bottom: "-=40px"} or {bottom: "+=40px"}

Any clarification or insights on this would be greatly appreciated.

Answer №1

Referencing W3C Guidelines

When both 'left' and 'right' properties are defined and not set to 'auto', the positioning is conflicting, leading one of them to be disregarded.

  • If the containing block's 'direction' property is 'ltr', the value of 'left' takes precedence while 'right' becomes -'left'.

  • If the containing block's 'direction' is 'rtl', 'right' prevails and 'left' is discarded.


This occurs due to the default left-to-right (`ltr`) direction in browsers, resulting in the ignorance of the `right` property.

Consider these scenarios :

  1. Upon clicking -> Right arrow, the div transforms as follows :

    <div class="box" style="right: -40px;">a box</div>
    
  2. Subsequently clicking on <- Left arrow, alters the div as shown :

    <div class="box" style="right: -40px; left: 0px;">a box</div>
    

    *Note - left:.. is appended after the right:.. property.

  3. Clicking -> Right arrow again results in,

    <div class="box" style="right: -80px; left: 0px;">a box</div>
    

    *The value of right:.. decreases by -40, but the left remains at 0 per application.

In this instance, the `right` property is overlooked - View Demo Fiddle.


To override the ltr with,

body  {
    direction: rtl;
}

the left attribute is dismissed. Experience Demo Fiddle

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

Strange behavior observed with mouseenter and mouseleave events in Firefox while using jQuery

My goal is to dynamically append a div element when the mouse enters an area and position it based on the mouse's coordinates. However, I am facing an issue where if the mouse enters close to the edge of the target element (e.g., within a quarter of i ...

Issue with record count in JqGrid after performing a search

Currently, I am utilizing jqgrid version 4.4. My goal is to determine the total number of rows in the grid. The following code is working well and accurately providing me with the record count: var Count = ($("#grid").jqGrid('getGridParam', &a ...

How to extract the YouTube video source using jQuery from an iframe snippet

Currently, I am attempting to extract the YouTube source from an iframe embed code and store it as a variable. Suppose my YouTube embed code looks like this: <iframe width=&quot;1024&quot; height=&quot;576&quot; src=&quot;https://w ...

Issues with fullcalendar refetchEvents and rerenderEvents in Safari and Opera browsers

After saving a dialog when creating a new event by clicking the day, I use the following code: addEvent(calEvent); // save to database when creating an event $dialogContent.dialog("close"); $('#calendar').fullCalen ...

Referencing a JSON object

Here is a JSON list of search terms: [ "halo", [ "halo reach", "halo anniversary", "halo 4", "halo 3", "halo mega bloks", "halo 2", "halo sleepsack", "halo wars", "halo reach xbox 360", "halo combat evolved" ], ...

Guide on using webpack to import jQuery

When using webpack, is it necessary to install the jquery file from npm, or can I simply require the downloaded file from the jquery website? directory app/ ./assets/javascripts/article/create/base.js ./assets/javascripts/lib/jquery-1.11.1.min.js webpack ...

Stop the content inside a div box from wrapping around the edges when using Bootstrap

Utilizing bootstrap to structure a webpage, I have organized three divs next to each other within a row. The widths of the divs are set as follows: 2 columns, 6 columns, and 4 columns in a 12 column layout. The middle div has a minimum width defined. As a ...

Click on the image to open it in the modal window

I'm looking for a way to display an image in a modal using Foundation. Currently, I have the following code: <img src="images/medium/Pluto.jpg" style="margin-left:10px;margin-right:10px; max-height:150px;max-width:150px"/> as well as <di ...

jQuery Table Sorting - Reorder Automatically Upon Page Initialization

I stumbled upon this amazing JS Fiddle. How can I tweak it to sort the third column in descending order when the page loads? $(document).on('click', 'th', function() { var table = $(this).parents('table').eq(0); var rows ...

javascript, issues with floating and displaying elements

I am currently working on creating a JavaScript menu that has a click function to smoothly slide up and down. Although the JavaScript code appears to be functioning correctly, it seems that there is some interference with the CSS. Despite attempting vario ...

Guide to customizing the CSS styles of various components within a Material UI Dialog

Let's take a look at this snippet for creating a Material Dialog in code: <Dialog open={this.props.open} onClose={this.props.closeAtParent} PaperProps={{ style: { minHeight: '75vh', minWidth: '75vw', ...

Is the "footer" div automatically nested within the "main" div if they are not nested in the code?

Within my code structure, I currently have the following: <html> <head> ... </head> <body> <div id="body"> <div id="main"> ... </div> <div id="foot ...

Every time Tailwind compiles, it seems to generate a glitchy class

There was a time when I accidentally wrote 2 classes without a space max-w-[412px]mb-[36px]. This caused an error, so I added a space between the classes. However, the error persisted even after making this correction. In a last attempt to resolve the issu ...

Adding CSS stylesheets on-the-fly in JavaFX

I am looking to incorporate a CSS file from the filesystem into my application. The goal is to allow users to dynamically add JavaFX CSS files that can be created by anyone and stored anywhere on their system. I attempted a test scenario to see if adding ...

"Trouble arises when a single quote is passed from the controller to the client-side MVC

In the Controller method, Serverside code is stored in ViewData with values like 'Double-Click to edit', '7C486', '7C489', '7C490', '7C491', and '7C492'. To display these values in a Dropdown wit ...

All you need to know about the jQuery .css method

I've been struggling to get this script to function properly. Despite searching online, I haven't been able to find a satisfactory solution. $( document ).ready(function() { $("#container").click(function(){ var color = $(this).css("backgro ...

Launch Bootstrap modal when clicking on an Iframe

I'm currently working on implementing a button that triggers a modal with an iframe inside. The goal is for the iframe to load only when the modal is opened. However, upon clicking the button to open the modal, nothing loads and there are no error mes ...

Making an element sticky within a container while the parent element is set to overflow hidden

Here is a case where I have generated HTML and CSS code that includes classes for overflow, container height, and sticky positioning: .overflow-hidden { overflow: hidden; } .overflow-x-auto { overflow-x: auto; } .container { max-height: 100px; ...

Including additional data to a page after each iteration in order to display the current progress

I am currently working on a loop that iterates through the lines of a text area and processes each line sequentially. However, I am facing an issue where the page becomes unresponsive until all the data has been processed. Is there a way to dynamically u ...

How to Pause or Temporarily Halt in Jquery?

Looking to lift the object up, wait 1000ms, and then hide it. I found this snippet of code: $("#test").animate({"top":"-=80px"},1500) .animate({"top":"-=0px"},1000) .animate({"opacity":"0"},500); However, using ".animate({"to ...