What is causing the left scroll to not work with negative values?

My design features three blocks stacked on top of each other with an additional block at the bottom below the middle.

Left <--------> Middle<---------->Right

-----------------Bottom------------------

I have a couple of queries.

  1. Why is scrolling to the left not working for negative values? (when clicking on section 2)
    (I am getting

    $('#section1').offset().left == -4000 
    )

  2. How can I utilize

    scrollTop: $($anchor.attr('href')).offset().top
    for navigation between top and bottom sections?

Visit JSFIDDLE link here

Here's the HTML code:

<div class="section white" id="section1" style='margin-left: -4000px;'>
            <h2>Section 1</h2>
            <ul class="nav">
                <li><a href="#section2">Right</a></li>
            </ul>
        </div>
        <div class="section black" id="section2">
            <h2>Section 2</h2>
            <ul class="nav">
                <li><a href="#section1">Left</a></li>
                <ii><a href="#section4">Bottom</a></ii>
                <li><a href="#section3">Right</a></li>
            </ul>
        </div>
        <div class="section white" id="section3">
            <h2>Section 3</h2>
            <ul class="nav">
                <li><a href="#section2">Left<</a></li>
            </ul>
        </div>
        <div class="section_vertical white" id="section4">
            <div style="width:33%; margin: 0 auto">
                <h2>Section 4</h2>
                <ul class="nav">
                    <li><a href="#section2">up</a></li>
                </ul>
            </div>
        </div>

Javascript code:

        $('ul.nav a').bind('click',function(event){
            var $anchor = $(this);
             $('html, body').stop().animate({
                  scrollLeft: $($anchor.attr('href')).offset().left
              }, 3000);
             event.preventDefault();
        });

Answer №1

Response 1

The left position of the body and html elements cannot be negative.

Instead:

1. Remove style='margin-left: -4000px;' from id="section1"
2. Add $('html, body').scrollLeft(4000) so that section 2 becomes visible

http://jsfiddle.net/buwn5yra/3/

Response 2

JQ:

$('ul.nav a').bind('click',function(event){
    var id = $(this).attr('href');
    var t=$(id).offset().top;  // get top position
    var l=$(id).offset().left; // get left position

    $('html, body').stop().animate({
        scrollLeft: l,
        scrollTop: t                        
    }, 3000);
    event.preventDefault();
});

HTML: In section4, remove the style "width:33%; margin: 0 auto". This style centers the content making it not visible when scrolling to this section as Section4 is supposed to be full width.

http://jsfiddle.net/buwn5yra/5/

Update for Response 2:

http://jsfiddle.net/buwn5yra/7/

$('ul.nav a').bind('click', function (event) {
    var id = $(this).attr('href');
    var t = $(id).offset().top;
    var l = $(id).offset().left;

    if(id=="#section4") l=$(this).parents('.section_vertical').left;

    $('html, body').stop().animate({
        scrollLeft: l,
        scrollTop: t
    }, 3000);
    event.preventDefault();
});


$('html, body').scrollLeft(4000)

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

When I hover my mouse over the items on the Navigation bar, they shift forward

Seeking assistance with a coding issue. When hovering over TOYOTA, the HONDA and CONTACT US sections move forward unexpectedly. This movement is unwanted as I would like only the next list item to display when hovering over TOYOTA and HONDA. How can I corr ...

How can I integrate the jQuery Plugin Mapael with Angular 5?

While exploring various methods that tackled the issue of integrating jQuery Plugins, I decided to start with the fundamentals. To begin with, I installed the jQuery plugin: npm i jquery Next, I included the TypeScript definition: npm install -d @types ...

What is the best way to iterate through elements that come after a specific element?

I'm confident that I will be able to provide an answer to this very soon... Here's a sample code snippet: <script> function ClearAndRunFuncs(element) { //Clears responses for elements AFTER this element with an onchange //Executes the uni ...

Maximizing the Benefits: Unlocking Potential with the WD Calendar's Dropdown

I recently came across this amazing WD calendar project that I'm incorporating into the project I'm currently working on. You can find it at this link: . However, I am facing a small challenge in remaking the "Add New Event" form as I can't ...

Aligning a Material UI button to the far right of a row

I am struggling to align a React material-ui button to the right-most of the page and despite trying to use the justify="flex-end" attribute within the following code, it doesn't seem to be working: <Grid item justify="flex-end" ...

The code functions perfectly on my local machine, however it is not cooperating on the HostGator server

A selection box based on values should appear in text fields, The current code is functional in local host but not working on the hostgator server For example, displaying country options based on the selected state and districts based on the selected sta ...

Obtaining the real file from the input with jQuery AJAX for uploading

Can someone help me with a file upload to Flask server using jQuery AJAX? My jQuery skills are lacking, and I'm struggling to fetch the file from the input field for my AJAX call. Here is the HTML: <form name="csvimportdataform" id=" ...

JSONP Error - "SyntaxError: Unexpected token caught"

Just to start off, I want to mention that I'm new to working with jsonp. This is my first time diving into the world of JSONP. Currently, I'm using a jQuery ajax call to retrieve data from a website. Here's a snippet of my jQuery code: $. ...

Should case sensitivity be disregarded in an array's input?

I need to search for a value in an array, but the search should be case-insensitive. How can I achieve this? This is the code snippet I am using: $.ajax({ type: "GET", url: "ajax.php", dataType: "json", data: { pageType: pageType, input: request.term, da ...

Troubleshooting issue: JSON.stringify function returning 'undefined'

Having some trouble with JSON in JavaScript. I've been using JSON.stringify without issue until now. But suddenly, when I try to use it in my application, I keep getting this error in the console (Google Chrome): Uncaught TypeError: undefined is not ...

Check out the findings from a real-time generated collection of ajax requests

As I work on creating a dynamic array of ajax calls to be performed, I find myself faced with an intriguing challenge. Here's how I currently approach it: var requests = []; if (weNeedCustomerData) { var customerCallPromi ...

Maintain the button's color when clicked UNTIL it is clicked again

I am facing a challenge where I need to dynamically select multiple buttons that change color upon click. Once a button is clicked, it should change color and if clicked again, revert back to its original color. Unfortunately, I cannot rely on HTML attribu ...

Combining id elements using jQuery

Here is the HTML code snippet: <div id="myDiv1_" class="formElement1"><asp:TextBox ID="TextBox1" CssClass="formInput1" runat="server"></asp:TextBox></div><br /> <div id="myDiv2_" class="formElement1"><asp:TextBox ID= ...

Move a <div> using a handle (without using JQuery)

I devised a plan to create a moveable div with a handle and came up with this code snippet: var mydragg = function() { return { move: function(divid, xpos, ypos) { divid.style.left = xpos + 'px'; divid.style.top = ypos + &apo ...

The text in my image is positioned away from the top

Hi there, I am new to exploring HTML and CSS and I have been trying to display some images and text on a webpage. My goal was to have the text appear next to the image, which is working fine. However, I am encountering an issue where the text aligns itself ...

Having trouble sending emails with jQuery 3, Ajax, and PHP mail form?

I have been working on updating a jQuery + PHP mail form to improve performance and standards compliance as I migrate the site to jQuery 3. However, I am encountering a persistent 500 Internal Server Error despite trying multiple methods. The existing lega ...

retrieve only the following occurrence throughout the entire file

I am looking to target only the first occurrence of an element in the document after a clicked element. Here is my current approach: $('.class').click(function(){ var x = $(this).nextAll('.anotherclass').first(); //do something ...

Exploring Alternatives to Hidden Fields in HTML

Similar Inquiry: Alternatives to Hidden Field in HTML Currently, I am utilizing a hidden field to conceal certain information on emails that are deemed confidential. Despite successfully extracting the hidden fields using Java, I encounter an issue wh ...

Creating dynamic content in the Ajax-enabled Smart Admin theme: A step-by-step guide

As I work on developing an application for a client, my focus is on creating a web service using Laravel5 for the backend. To enhance the user interface of this web service, I have chosen to incorporate the Smart Admin Theme, specifically utilizing the Aja ...

Effortlessly sending multiple values from text fields using jQuery

i am using jQuery to fetch all values from text fields with the same field name. $('input[name^="StudentName"]').each(function() { StudentName += $(this).val(); alert(StudentName); }); when I alert, all the values are displayed as one s ...