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

What is the best method for implementing border-radius on select2 elements?

I previously had a default bootstrap user select box with a border-radius style applied: <div class="container"> <select class="form-control" style="border-radius: 1rem;"> <option value="1">Us ...

How can you make a dropdown button interactive and display a list simultaneously?

I'm facing an issue with lines 45-47 in the second link. If that code is present, the button animates when clicked (which is great) BUT the dropdown items ("About," "Archive," "Contact") do not appear. If I remove lines 45-47, the dropdown items appea ...

Detecting characters in jQuery's onKeyPress with case sensitivity

Is there a way to detect characters in a "case-sensitive" manner using the onkeypress event in jQuery? ...

Tips on clearing notices and warnings at the bottom of a PHP questionnaire

Below is the code I am working with: <?php include_once 'init/init.funcs.php'; $_SESSION['pollid'] = (int) $_GET['pollid']; $questions = array(); if (!isset($_SESSION['answering'])) { $result = mysql_query ...

Incorporating hyperlinks within the navigation buttons

I'm working on a design where I have six buttons that need to be displayed on top of a background image. How can I ensure that the text inside the buttons stays contained within them? Here is the current code structure I am using, which involves utili ...

What could be causing my iframe to not adjust its size according to its content?

My attempt at resizing a cross-site iframe is not working as expected, despite following the guidance provided here. The iframe on my page seems to remain unaltered in size, and I can't figure out what mistake I might be making. The current location ...

Why does my Node.JS Express request return undefined after submitting a post request from a form?

Having trouble retrieving data from an HTML form using Node.JS/Express. The req.body seems to be empty, returning an undefined value. Node const express = require('express') const bodyParser = require('body-parser') const app = express ...

php code to paginate mysql results

Imagine I have 50 rows in my database. How can I retrieve MySQL results in pages, displaying 5 results on each page and showcasing the pages as follows: [1], 2, 3, 4...10? For example, if it's on page 5, show 3, 4, [5], 6, 7...10 without refreshing al ...

What is the best way to prevent right floated DIVs from interfering with each other's positioning, specifically within a specified space?

Despite feeling like this question may have been asked numerous times before, I've searched high and low for an answer to no avail - both here and on Google. My HTML page has a maximum width that limits the size of the content. This content includes ...

Tips for designing a sophisticated "tag addition" feature

Currently, I am enhancing my website's news system and want to incorporate tags. My goal is to allow users to submit tags that will be added to an array (hidden field) within the form. I aim to add tags individually so they can all be included in the ...

Tips for avoiding the page from reloading when executing a query. I attempted using preventDefault(); which successfully prevented the reload, but it also stopped PHP

function createCounter() { var count = 0; return function () {return count += 1;} } var add = createCounter(); function updateDatabaseOnClick(){ document.getElementById("result").innerHTML = add(); } I'm currently working on implementing a co ...

Error encountered while making a Jquery Ajax request to Rest service

I have created a RESTful Web Service: import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("/todo") public class TodoResource { @GET @Produces({MediaType.APPLICATION_JSON}) public To ...

Developing web applications using a combination of PHP, MySQL, and

I am in need of creating an HTML form that functions as CRUD. This form should be able to record inputs such as "row_number", "channel_name", and "ip_address". It will store all the data in a MySQL table using the "ORDER BY row_number" function. The form i ...

How to create a bottom border in several TD elements using a combination of CSS and jQuery

Check out my Website: Search Page I have written some CSS that functions well when searching by Name, but not by Specialty: .displayresult { display: block; } #fname, #lname, #splabel, #addlabel, #pnlabel { border-width: 4px; border-bottom-st ...

Substitute the temporary text with an actual value in JavaScript/j

Looking to customize my JSP website by duplicating HTML elements and changing their attributes to create a dynamic form. Here is the current JavaScript code snippet I have: function getTemplateHtml(templateType) { <%-- Get current number of element ...

The function Mediarecorder.start() is experiencing issues on Firefox for Android and is not functioning

Recently, I've been facing a peculiar issue while developing a web application. The purpose of this app is to capture about 10 seconds of video intermittently from the device webcam and then upload it to a server. For this functionality, I utilized th ...

Kendo UI data source not activating transport for :Create operation

Currently, I am encountering an issue with my UserEventGrid method in which the create command is not being triggered. The read command works perfectly and populates the grid as expected. The situation is that upon clicking the Add button on the main grid, ...

Having multiple HTML select elements and utilizing jQuery AJAX

I am looking to implement a dynamic multiple select using AJAX and jQuery. The first select (c1) is functioning correctly. When I click on it, it triggers the appearance of another select (c2). Similarly, clicking on the second select (c2) should lead to ...

use jquery to automatically invoke a function with the .animate method

I've hit a snag in my jQuery project and could use some help. I'm currently utilizing jQuery's .animate() function like so: $(".planeid").animate({top:'590px'},1000); with an initial top value of 0. Is there a way to initiate a ...

A fresh checkbox was added to the page using Jquery Switchery to disable it

I'm having trouble disabling the Switchery checkbox. When I try to disable it, another Switchery checkbox appears on the page along with the one I previously defined: <div class="form-group"> <label class="col-md-2"> ...