Scrolling in a smooth horizontal motion

I need to create a horizontal website where each section takes up the entire screen. I have added anchors to navigate between sections using links, but I am struggling to make a JavaScript code work for smooth horizontal scrolling.

Here is my HTML:

    <body>
    <header class="section black">
    <a href="#mission">SCHOOL MISSION</a>
    <div class="logo"></div>
    </header>

    <section class="section gray" id="mission">
    <p>
    Second section
    </p>
    </section>
    </body>

And here is my CSS:

* {
margin: 0;
}

body {
  width: 2000px;
position: absolute;
top:0px;
left:0px;
bottom:0px;
}

.section {
margin: 0px;
bottom: 0px;
width: 100vw;
float: left;
height: 100vh;
}

.black {
background-color: #000000;
}

.gray {
background-color: #838B8B;
}   

Check out an example here: https://jsfiddle.net/mnn94crj/10/

Answer №1

Enhance the behavior of your target links by implementing a jQuery function and incorporating animate for smooth scrolling effects.

$(document).ready(function(){

    $('a').each(function(){ // Iterate through each link
        var href = $(this).attr('href');
        if (href[0] !== "#") return;
        $(this).on('click', function(){ // Define click function
            $('body, html').animate({scrollLeft: $(href).offset().left});
        });
    });

});

Visit this JSFiddle demo for more information.

Answer №2

Hey there! Feel free to grab the following JavaScript file and insert it within the head section of your website:
http://examplewebsite.com/js/jquery.carousel.js

To enable smooth scrolling, simply add the code snippet 'anchorlink' inside your <a> tag.

<a href="#about" class="anchorLink">ABOUT US</a>

Answer №3

Take a look at this revised JSFiddle showcasing the use of jQuery (animate):

JavaScript

$("#mission-link").on("click", function(){
  $('body').animate({scrollLeft: $("#mission").offset().left}, 800)
});

HTML

<body>
  <header class="section black">
    <a href="#" id="mission-link">SCHOOL MISSION</a>
    <div class="logo"></div>
  </header>
  <section class="section gray" id="mission">
    Mission
  </section>
</body>

Answer №4

Your question has been tagged with Jquery so here is the solution using jquery.

$("button").on("click", function(){
  var selectedElement = $(this).attr("id");
  $('html, body').animate({scrollTop: $("#" + selectedElement).offset().top}, 1000);
  return false;

});

Additionally, a similar question has already been asked and answered here: jquery: animate scrollTop

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

"Enhance your web development with Vue.js and Vue-chart.js for beautiful linear

I'm currently struggling to implement a linear gradient background on my Vue-chart.js line chart. Despite searching high and low, the documentation and examples available are not proving to be helpful. After importing the Line component from vue-char ...

Looking for assistance in translating HTML code into an XpathQueryString?

While following a tutorial by raywenderlich on how to parse HTML in Xcode, I encountered an issue. I was able to parse his website successfully, but I'm struggling with the concept of Xpathquery. All I need is to extract the data from a table. Below i ...

The custom radio button is not showing up on iOs Safari

My code for custom radio buttons using CSS is as follows: input[type=radio]{ display:none; } .radio-options label::before { content: "\2b24"; color:#e2e2e2; display: inline-block !important; width: 17px; height: 17px; vert ...

"Transforming a jQuery UI dialog into a Bootstrap modal: A step-by-step guide

How can I transform this element into a Boostrap UI jQuery object? <div id="dialogDeleteDefaultVariant" title="default"> <p><span style="margin: 0 7px 20px 0;"></span>' . HTML::button('dialog_delete_default_variant_des ...

Challenges in integrating callbacks effectively in node.js scripts

I am currently working on a node.js function called process() that is meant to return a value when it is invoked. However, I am encountering difficulties in creating a callback for the process(). The expected behavior is for the value to be returned from p ...

Creating a static HTML page using Flask and incorporating redirects

I have a web application built using Flask that processes a URL, performs some operations, and then redirects the user to another URL. I am looking for a way to display a simple "please wait" message in a static HTML page while the processing is happenin ...

Is it considered safe to modify variables by using this[varName] = something within a function that includes varName as a parameter?

As I continue working on this function, a question arises regarding the safety of changing variables in this manner. In my Angular service, I utilize utility functions where context represents this from the component calling the function. The code snippet ...

Animate the CSS position from its current location to the bottom, set at 0%

I am trying to create a CSS animation to slide up an image that is sticking out of the bottom of the screen. The challenge is that the dimensions of the image are variable, so I don't know how much of it is initially visible outside the screen. What ...

The process of linking .then functions and triggering a success callback function in Angular.js

I am attempting to chain nested .then functions and execute the success functions, but the callback is being triggered at the beginning. //public method fn function fn(callback) { //calling the 1st API request fn1() .then(function(response) { //2nd ...

The presence of double quotes in stringified JSON is undesired

I am currently working on a weather forecasting website where the API returns pure JSON data. However, when I convert this data to a string and add it to an element in order to display it on the webpage, I encounter a problem with double quotes appearing. ...

Utilizing Cordova and AngularJS to efficiently retrieve JSON data through XMLHttpRequest in a factory

I've encountered an issue while attempting to retrieve a JSON from an API using XMLHttpRequest within a factory. Despite the debug logs suggesting that everything is functioning correctly, I keep getting an "undefined" response. Below is the code for ...

Ways to perfectly align an icon both horizontally and vertically in an <a> tag that covers the entire container

Looking to center an icon within an "a" tag that spans the entire div container? The goal is to make the entire container clickable for carousel navigation, rather than just the icon itself. Currently, I can achieve each desired effect separately. On the ...

"Exploring the possibilities of combining jQuery Touch Punch with the power of css

I'm facing an issue with jQuery UI Touch Punch. I am able to drag everything smoothly, but when I apply css3's "translateY", it works perfectly on the computer but fails on iPad. The element suddenly jumps up at dragstart and then allows me to dr ...

Preventing a sprite from going beyond the boundaries of a canvas with pure JavaScript - here's how!

I am in the process of developing a game using native JavaScript, HTML, and a touch of CSS. I have two blocks called Sprites that tend to keep moving off the canvas edges when they reach them. Question: How can I utilize native JS to prevent the sprites fr ...

How come using a query object as a parameter for .limit() returns an empty array?

I am currently working on a mongoose query setup where I have a custom queryObject containing key-value pairs for specific records. If a key-value pair does not exist in the req.query, it is omitted from the queryObject. Oddly enough, when setting the que ...

Improve code efficiency by streamlining a function and using more effective syntax techniques

I've been learning how to condense code with jQuery. Can this script be written in a more concise manner without everything being on one long line? items.push('<li id="' + key + '">' + ' (key: ' + key + ')&apo ...

"Exploring the Power of Jsoup for Selecting

I'm attempting to extract all the links nested within the div class news column index. Here is a snippet of the HTML structure: https://i.stack.imgur.com/zdFWS.jpg Below is the code I have used, but unfortunately, it's not yielding any results. ...

Is there a way to modify the edit button to become a save button and include a cancel button?

Is there a way to toggle between an edit button and a save button, along with a cancel option? $('#edit').click(function() { $(this).hide(); $('#save, #cancel').show(); }); $('#cancel').click(function() { $('#ed ...

Filtering Jquery datatable results by text

In order to filter records from a jQuery data table, I have utilized the following code. The format for my data table is as follows: var aDataSet = [['1', 'GOld', 'G-110,G-112,G-123', 'G1-001,G1-005,G1-008'], ...

What is the method for retrieving the data shown in a KendoGrid in JSON format?

I am working with a kendoGrid and my goal is to extract the filtered and sorted data in JSON format. How can I accomplish this? For example, something along these lines, var grid = $("#grid").data("kendoGrid"); alert(grid.dataSource.data.json); // I hav ...