Horizontal sliding layout with mousewheel functionality for responsive design

I am currently working on incorporating a sliding horizontal layout with a header banner.

Here is the HTML structure I am using:

<body>
  <div id="header">
    <div><a href="#one"> one </a></div>
    <div><a href="#two"> two </a></div>
    <div><a href="#thr"> thr </a></div>
  </div>

  <div id="one" class="panel"> </div>
  <div id="two" class="panel"> </div>
  <div id="thr" class="panel"> </div>
</body>

The header remains fixed, and the panels have been designed with a gradient background (the middle panel has a different color for debugging purposes). Here is the CSS styling being used:

    body {
      width: 6000px;
      overflow: hidden;
    }

    .panel {
      width: 33.3%;
      float: left;
      padding-left: 30px;
      padding-right: 1040px;
      margin-top: -75px;
      height: 960px;
      background-image: linear-gradient(to bottom, #0B88FF, #95EEFF);
    }

    #header {
      position: fixed;
      height: 75px;
      margin-top: 25px;
    }

    #two{
      background-image: linear-gradient(to bottom, #0B8800, #9E5EFF);
    }

Additionally, I have a function in place to handle the animation between panels:

$(document).ready(function() {
  $("#header a").bind("click", function(event) {
    event.preventDefault();
    var target = $(this).attr("href");
    $("html, body").stop().animate({
      scrollLeft: $(target).offset().left,
      scrollTop: $(target).offset().top
    }, 1200);
  });
});

However, I am encountering a few issues that need troubleshooting:

1) I have attempted to implement a jQuery function for a slide animation triggered by the mouse wheel, but my tests have not been successful...I am consistently encountering the same issue:

$(window).scroll(function() {
      if ($(this).scrollTop() > 0) {
        var target // I am still unable to determine the appropriate target
        $("html, body").stop().animate({
            //should be moving towards the target >,<
       }
});

2) The layout appears to function correctly when my browser window is at 100% size, but adjusting the zoom causes everything to become disordered >,< I am aware that this can be resolved, and I found an example showcasing the solution: here is the link

Answer №1

If you want to achieve your goals, one approach is to populate an array with elements that have the class of panel, and then utilize an index to navigate through these panels.

Additionally, in case you encounter issues with scrolling when the window is resized, you can listen for and handle the resize event to perform necessary actions.

For more information, refer to the documentation on MouseWheelEvent.

Give this example a try with your own code:

$(document).ready(function() {
  $("#header a").bind("click", function(event) {
    event.preventDefault();
    var target = $(this).attr("href");
    $("html, body").stop().animate({
      scrollLeft: $(target).offset().left,
      scrollTop: $(target).offset().top
    }, 1200);
  });
  
  var scroll_targets = $(".panel");
  var scroll_targets_index = 0;
  $(window).on('DOMMouseScroll mousewheel', function (e) {    
    if (e.originalEvent.wheelDelta < 0) {
      if(scroll_targets_index < scroll_targets.length-1){
        var where = ++scroll_targets_index;
        $("html, body").stop().animate({
          scrollLeft: $(scroll_targets[where]).offset().left,
          scrollTop: $(scroll_targets[where]).offset().top
        }, 1200);
      }
    } 
    else {
    var where;
    if(scroll_targets_index > 0){
       where = --scroll_targets_index;
      }
      else{
      where = 0;
      }
      $("html, body").stop().animate({
          scrollLeft: $(scroll_targets[where]).offset().left,
          scrollTop: $(scroll_targets[where]).offset().top
        }, 1200);
      
    }
  });
  
  $(window).resize(function () {
    $('html,body').scrollTop($(scroll_targets[where]).offset().top);
    $('html,body').scrollLeft($(scroll_targets[where]).offset().left);
  });
});
#body {
      width: 6000px;
      overflow: hidden;
    }

    .panel {
      width: 33.3%;
      float: left;
      padding-left: 30px;
      padding-right: 1040px;
      margin-top: -75px;
      height: 960px;
      background-image: linear-gradient(to bottom, #0B88FF, #95EEFF);
    }

    #header {
      position: fixed;
      height: 75px;
      margin-top: 25px;
    }

    #two{
      background-image: linear-gradient(to bottom, #0B8800, #9E5EFF);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="body">
  <div id="header">
    <div><a href="#one"> one </a></div>
    <div><a href="#two"> two </a></div>
    <div><a href="#thr"> thr </a></div>
  </div>

  <div id="one" class="panel"> </div>
  <div id="two" class="panel"> </div>
  <div id="thr" class="panel"> </div>
</div>

Answer №2

When it comes to Unravel, I believe the use of CSS and JavaScript, particularly transform3d and various CSS techniques, played a significant role. However, encountering issues with getting the scroll event while having overflow:hidden on the body can be problematic as it hides all scrolls. To address this issue, I resorted to utilizing the mousewheel event, which functions in the following manner:

$(window).on('wheel', function (event) {      
    console.log(event); // Detailed event properties and functions are visible in the console
});

The wheelDelta property is worth noting as it returns 120 for forward mousewheel movements and -120 for backward ones. By incorporating a counter, I could track the number of times the mousewheel event was triggered:

$(window).on('wheel', function (event) {      

    if (event.originalEvent.wheelDelta / 120 > 0 ) {
        count++;
        console.log(count);
        if(count > 30 ){

            $("html, body").stop().animate({
              scrollLeft: $('#two').offset().left,
              scrollTop: $('#two').offset().top
            }, 1200);   
        }
        if(count > 60 ){

            $("html, body").stop().animate({
              scrollLeft: $('#thr').offset().left,
              scrollTop: $('#thr').offset().top
            }, 1200);               
        }
        if(count > 90 ){

            $("html, body").stop().animate({
              scrollLeft: $('#two').offset().left,
              scrollTop: $('#two').offset().top
            }, 1200);   
            count = 0;          
        }


    }
});

These steps are designed to aid you in developing the logic for transitioning between panels based on mousewheel actions. Best of luck with your project!

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

Text hidden within the image, each line concealing a separate message

I am having an issue where my image is overlaying the text. I would like the text to wrap around the image and for any hidden text to appear on a different line. How can this be achieved? Here is the CSS code I am using: div.relative { position: relati ...

dilemma with jquery slideshow

I have a unique challenge where I want to display different images for each month of the year. My goal is to create a slide show that will showcase the images related to the selected month on top of the page. However, when I click on a specific month, I en ...

Tips for ensuring long text wraps to the next line when it exceeds the width of the browser window

I'm struggling with CSS styles and properties. I want the text to wrap to the next line instead of overflowing off the browser screen. I've illustrated what's currently happening versus what I actually need. https://i.stack.imgur.com/mPGt8 ...

Creating a triangle shape using Bootstrap to style a div element, similar to the image provided

Trying to achieve the look of the attached image with a few divs. I know I can use a background image like this: background:url(image.png) no-repeat left top; But I'm curious if there's another way to achieve this without using a background ima ...

Adjust the alignment of two headers with varying sizes

Can someone provide guidance on aligning two headers of different sizes (e.g. h3 and h5) based on their bottom edges, making them appear like a cohesive group in a sentence format? The current code snippet I am working with is as follows: ...

Trouble with DOM loading due to external JavaScript file reference

I'm having an issue with referencing the jQuery library in a master page. Here is how I am including it: <script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"> After loading the page, I only see a blank screen. The HTML code is th ...

The use of multiple Where clauses in a Firestore Firebase query is not functioning as expected when implemented in JavaScript code

https://i.stack.imgur.com/DdUGj.png db.collection('User_Info').where("User_Name", "==", "Sam").where("PASSWORD", "==", "c2FtMTIzQA==").get().then(snapshot => { if(snapshot.docs.length > 0 ){ debugger; alert("Login Successful."); ...

Attempting to enlarge an image by hovering over it proves to be futile

Currently, I am exploring CSS and attempting to enlarge 2 images when they are hovered over. Unfortunately, the desired effect is not working. .imagezoom img { position: relative; float: left; margin: 50px 5px 0px 42px; border-color: rgba(143, 179, 218, ...

What is the best way to position a rectangle on top of a div that has been rendered using

I recently started using a waveform display/play library known as wavesurfer. Within the code snippet below, I have integrated two wavesurfer objects that are displayed and positioned inside div elements of type "container". My goal is to position my own ...

Trick for using :checked in CSS

Is it possible to change the color of a deep linking div using an input checkbox hack in the code below? <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /&g ...

Dynamic CSS overlay based on database field content without requiring user interaction

Is there a way to automatically display overlays for specific options in a database field using Bootstrap code? The options are SOLD, CONTRACT, and NO. I need different overlays to appear for SOLD and CONTRACT, while no overlay is needed for the NO option ...

Exploring the world of CSS: accessing style attributes using JavaScript

So I created a basic HTML file with a div and linked it to a CSS file for styling: HTML : <html> <head> <title>Simple Movement</title> <meta charset="UTF-8"> <link rel="stylesheet" type=&qu ...

Find the size of the grid using the data attribute

I am currently working on a piece of code that involves fetching a data-attribute known as grid size from the HTML. My objective is to create a conditional statement that checks whether the value of grid size is "large" or "small", and assigns specific x a ...

Disable the borders on HTML input fields

I am looking to implement a search feature on my website. It seems that in Firefox and Internet Explorer, the search function appears fine without any unexpected borders. Firefox IE However, when viewing the search function in Chrome and Safari, there ...

Sliding out the existing content and smoothly sliding in a fresh one upon clicking

Thank you for taking the time to read this. I've created a navigation bar and a few DIVs. What I want to achieve is when a button is clicked, the current DIV slides to the left and out of the page, while a new DIV slides in from the right. [http://w ...

The linking process in AngularJS is encountering difficulties when trying to interact with

I've already looked at similar questions, and my code seems correct based on the answers provided. It's a very simple beginner code. <html ng-app=""> <head> <title>Assignment</title> <script src=" ...

When scrolling, the text or logo inside the navbar seems to dance with a slight wiggling or

I recently implemented a code to create a logo animation inside my navigation bar that expands and contracts as I scroll, inspired by the functionality of the Wall Street Journal website. However, this implementation has caused some unintended side effects ...

HTML and CSS link conflict

As a beginner in CSS, I am experimenting with creating a simple website using HTML and CSS. However, I have encountered an issue where the links on the left side of my page are being interfered with by a paragraph in the middle section. This causes some of ...

Switching effortlessly between Fixed and Relative positioning

As I work on creating a unique scrolling experience, I aim to have elements stop at specific points and animate before returning to normal scroll behavior once the user reaches the final point of the page. Essentially, when div X reaches the middle of the ...

Resolving conflicts between Bootstrap and custom CSS transitions: A guide to identifying and fixing conflicting styles

I am currently working on implementing a custom CSS transition in my project that is based on Bootstrap 3. The setup consists of a simple flex container containing an input field and a select field. The functionality involves using jQuery to apply a .hidde ...