What is the method for scrolling left in HTML?

I'm in the process of creating a one-page website that includes standard sections like services, portfolio, contact, and blog. When users click on the navigation menu for 'services', 'portfolio', and 'contact', it smoothly scrolls to the desired section without any issues.

My unique idea is to implement a different scrolling effect when users navigate to the blog section - instead of the typical up and down scroll, I want it to scroll horizontally from right to left for an engaging user experience. Currently, this functionality is partially working but there's a small problem. When users are at the bottom of the page (e.g., the contact section) and try to navigate to the blog, it first scrolls to the top before moving horizontally to display the blog section.

To improve this, I aim to eliminate the unnecessary vertical scrolling to the top and just smoothly transition into the blog section with a horizontal scroll from the point where users are currently positioned.

Do you have any suggestions on how to achieve this seamless sideways scrolling effect? Or should I consider separating the blog section into its own independent HTML file?

Edit

http://jsfiddle.net/VS9dS/20/

Scrolling from the blog to any section or navigating between sections vertically:

$('html, body').animate({scrollLeft: -$(sectionName).offset().left}, "slow");
$('html, body').animate({scrollTop: $(sectionName).offset().top}, "slow");

Scrolling from any section to the blog:

$('html, body').animate({scrollTop: $(sectionName).offset().top}, "slow");};
$('html, body').animate({scrollLeft: $(sectionName).offset().left}, "slow");

I haven't included much code due to time constraints. Currently, my code functions in a way that if a user is on the contact section and clicks on the blog link, it scrolls to the top and then transitions horizontally to the blog as if it's a separate HTML page. My goal is to refine this behavior so that clicking on the blog from any section instantly shows the top of the blog without unnecessary vertical scrolling.

Answer №1

If my understanding is correct and you simply wish to animate one element before another, you can achieve this with the following code:

$('html, body').animate({
    scrollTop: $(sectionName).offset().top
}, "slow", function () {
    $('html, body').animate({
        scrollLeft: $(sectionName).offset().left
    }, "slow");
});

...or if you prefer to scroll to the right after scrolling to the top, you can use this code snippet:

$('a[href=#blog]').click(function () {
    $('html, body').animate({
        scrollTop: $('#home').offset().top
    }, "slow", function () {
        $('html, body').animate({
            left: '-=200px'
        }, "slow",'linear'); //possibly a smoother effect
    });
});

CSS:

body {
    position: relative;
}

Fiddle: http://jsfiddle.net/Matze/eRNde/

http://api.jquery.com/animate/#basic-usage

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

I'm trying to position this div within a table so that it lines up with one of the cells. Can you help me figure out

After implementing the code, here is the current output: <table><tr> <td><div id="adt"></div></td> <!-- 336x280 ad code --> <td id="butw"><div id="butto">Follow @Twitter</div></td> ...

Performing addition in Javascript can sometimes yield unexpected results, especially when dealing with numbers

Recently, I delved into the realm of cookies for the first time and managed to successfully save some data. The numbers I stored are crucial because I need to perform arithmetic operations like addition and subtraction on them. However, when attempting to ...

Ways to correct misaligned Bootstrap column grid

I'm currently working on a form using Bootstrap 4, but I'm facing an issue where the columns are not aligning properly, causing unwanted space and reducing the size of the input boxes. Despite trying different Bootstrap CSS CDN codes in addition ...

Guide on creating a hover-based side menu

I am looking to enhance the sub-menus like PRODUCT 1, PRODUCT 2, etc. by adding side menus that appear when hovering over them. Can anyone assist with implementing this feature in the code below? Below is the current code snippet: CSS: @import url(http: ...

Change the color of all the text on a page to a designated shade

Is there a way to change the color of all text on a page to a specific color? I am dealing with user-generated content in HTML and CSS, making it impossible to control classes for specific elements. Given the wide variation in the page's HTML an ...

The hamburger icon on the Bootstrap 4 navbar expands as expected, yet it fails to collapse back when

<head> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src=" ...

AngularJS - Choose and establish initial values for Editing or Creating New Items

My first project involving AngularJS has left me a bit stuck when it comes to using the select list. I need to either set the default value to the first option for a new entry, or if it's an edit, select the appropriate value. In my form, there are t ...

Organize your Django form with visually distinct groups for better user experience

I am currently working on a form within my Django application that contains multiple fields. In order to enhance the user interface and make it more intuitive, I am looking for ways to group the fields into sections. This could be achieved by enclosing th ...

Attempting to create a redirection landing page

I have a process where I create a new user and save it in my database with the following code snippet: const newUser = new User({ username: userId, password: pass, nameOfUser: user_name, emailOfUser: user_email ); newUser.save(); res.redir ...

What are the steps for incorporating information into a designated PhpMyAdmin account?

Currently, I am experimenting with HTML and Php. The website is quite simple at the moment and lacks adequate security measures. My objective is to update a specific user's competition field in the users table located within the authentication folder ...

Sending Parameters Using Higher Order Functions

Utilizing React and attempting to pass multiple arguments along with the 'event', I opted to implement a Higher Order function for this purpose. Unfortunately, it seems that the 'id' passed to the Higher Order function is not being rec ...

Discover the xpath of an element while bypassing any internal elements

My HTML structure is quite complex, with numerous tables and divs. Additionally, this structure may change over time. Is there a way to find the XPath while skipping elements in between? <table> <tr> <td> <span>First Na ...

Create interactive HTML tables with detailed information using PHP and MySQL databases

I am attempting to achieve a similar result as shown here: but with the addition that my HTML tables will be filled dynamically. The scenario involves two tables: +------------------------+ | TB_ACTIVE_USERS | +------------------------+ | ID | ...

Tips for navigating a list of areas in JavaScript

Currently, I am in the process of learning Javascript and I have a query regarding browsing an area list using Javascript. Could someone kindly guide me on whether it is possible to achieve this, and if so, how? Below is the HTML code snippet I am workin ...

Attempting to access an HTTP Node server from within an HTTPS environment resulted in an SSL error

I am faced with the challenge of connecting to a socket.io from a React client within a Node server. Both the React client and the location of the Node server (a microservice housed in a separate Docker container alongside a Java container) are operating o ...

"The Promise in the AngularJS Karma test specification did not resolve and the .then() method was not invoked

An issue arises when attempting to perform AngularJS Karma Unit Testing on a service. The service includes a method like the one below: service.getIntersectingElements = function (element, elements) { var deferred = $q.defer(); var tolerance = 20 ...

Trouble with setting up Git Hub Repositories through Ajax JQuery

I am having trouble creating a GitHub repository using AJAX from JQuery. Here is the code I am using: $.ajax({ url: "https://api.github.com/my-user/repos", headers: { 'Authorization' : 'token my-token' }, method: &apos ...

Vue component not responding to @click event

I have a Vue component where I have written a function to increase the value of likes by 1 when a button is clicked. However, this functionality doesn't seem to be working as expected. I initially tried writing it directly inside the component like th ...

Is Chrome prone to memory leaks when utilizing a Shared Worker?

I have encountered an issue with my web page that triggers a HTML5 SharedWorker script. Interestingly, each time I refresh the page (using F5), the memory usage in Chrome increases. The worker script is quite basic. It simply sends a message to the connec ...

What are the best methods for assigning different characteristics to cubes using three.js?

Hello! I am currently working on a project using three.js. My goal is to create four cubes and achieve the following tasks: (1) apply a texture from another image onto one cube, (2) make light reflect off of a specific cube, and (3) make one cube transpare ...