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

Background image not showing on div element

As someone who is new to coding, I could really use some assistance with a problem I've encountered. Specifically, I'm attempting to reference an image within a div element, but unfortunately it's not displaying when I check through my brows ...

Using jQuery's ajax function to send data with a variable name of data field

I am trying to figure out how to dynamically add a variable to the name of the data field I want to send information to through ajax. Below is an example of the code I'm working on: var qty = $('#qty_'+value).val(); $.ajax({ url: &apo ...

Having trouble placing the flash element above the HTML?

Despite my efforts to use SWFObject to embed a swf file on my webpage, it seems that the flash movie is covering some of the HTML components. Strangely enough, the HTML elements are bleeding through and appearing on top of the flash content. I've tri ...

Delicate seams break up the different sections of the webpage

I'm facing an issue with my website where thin lines are appearing between each section when previewed in Safari and Chrome, but not in Firefox (as shown in the images). I've checked the CSS code for each section in Dreamweaver, where the lines d ...

Creating a Vue application without integrating Bootstrap into the build process

Currently, I am in the process of learning how to develop a Vue app, but I am encountering some difficulties regarding Bootstrap and JQuery integration. The platform where the app is meant to be deployed already has Bootstrap and JQuery integrated into it ...

Pass multiple variables as input to a function, then query a JSON array to retrieve multiple array values as the output

My JavaScript function contains a JSON array, where it takes an input and searches for the corresponding key/value pair to return the desired value. I am attempting to input a string of variables like this: 1,2,3,4,5 Into this function: function getF(f ...

Issue with jQuery 'on' event not triggering following 'load' event

I am facing an issue on a page where similar events occur but when new content is loaded halfway through, most of the jQuery functionalities stop working. The scenario involves answering questions in a 'game' format using AJAX calls. Once all que ...

Toggle Divs with jQuery (Rotate images depending on link)

I am currently using the following code to toggle the visibility of div elements. http://jsfiddle.net/XwN2L/3120/ In addition, I would like to have a unique image sprite for each link when it is selected. I attempted to assign individual IDs to each link ...

Is there a way to create a Swipe slideshow without relying on plugins or external tools?

How can I create a responsive swipe image slideshow using CSS and JQuery that changes the image when swiping from right to left? I want it to work seamlessly on both computer and mobile screens. ...

"Is there a way to adjust the range slider to display currency instead of

I stumbled upon this amazing slider on codepen. Can someone guide me on how to adjust it to display a range from €500 to €6000 while keeping the vibrant red background? I've attempted various solutions like: <input id = "range" type = "range ...

Is it possible to disable the save option on a PDF popup window?

When displaying a PDF using an embed tag, I have managed to disable print and content copying through document properties. However, I am struggling to find a solution to disable the save option that pops up. Is there a way to achieve this using JavaScrip ...

Creating an anchor element with text and a bootstrap icon involves using the appropriate HTML structure and

I am trying to figure out how to create an element that includes both text and a Bootstrap icon. Here is the HTML code: <a> Edit <i class="icon-pencil" /> </a> Can anyone help me understand how to select this DOM element using jQuer ...

I am looking to have three rows of input text area comments instead of just one

Utilizing Bootstrap 4.3.1 with React Components I currently have this image: https://i.sstatic.net/rudsE.png And I am aiming for this design: https://i.sstatic.net/VmyTW.png comments: { elementLabel: "Comments", elementType: 'textar ...

What are some top-notch Responsive Lightbox or Modalbox options similar to those found on Facebook or Instagram?

Is there a recommended jQuery plugin that can help me create a gallery with images, comments, and descriptions similar to the layouts on Facebook and Instagram? ...

Placing 2 elements next to each other - Where the left element remains static and the right element's width increases as the page expands

Hey there! I'm really struggling to position two elements, an aside and a section (I believe the use of these HTML5 elements is important for their content). On this page Click Here, my goal is to keep the 'Locations' (Aside) element static ...

The presence of '<!DOCTYPE html>' causes my website's content to compress

After including !DOCTYPE html, I noticed that the first section of my website is shrinking even though its height is set at 120%. I attempted various solutions from stack overflow like setting the height of HTML and body to 100%, but none of them resolved ...

Tips for effectively utilizing the display:none property to conceal elements and permanently eliminate them from the DOM

While working on my website, I utilized CSS media queries to hide certain elements by using display: none. Even though this effectively hides the element from view, it still lingers in the DOM. Is there a way to completely eliminate the element from the ...

Maintain the expanded menu even after selecting a sub-item using jQuery

After conducting a thorough search, I was unable to find exactly what I needed. I have successfully implemented cookies on my menu so that when the page is reloaded, it remembers which menus were open. However, I noticed that clicking on a sub-item of Hy ...

Using Jquery to Insert Numbers Inside Brackets to Selection

I am struggling to calculate the sum of numbers in jQuery for selection fields. Currently, my code looks like this. $(function() { $("select").change(function() { updateTotal(); }); updateTotal(); }); function updateTotal() { ...

Is it possible to alter the background behind each word in a text using only CSS?

I have a question about styling quotes by highlighting each word in the text with a background color. The issue I'm facing is that when using a solid background, there are no gaps between the colors. How can I achieve a result similar to this, but wit ...