Navigating between divs with a 100% height using up and down movements

I am working on a website that is structured into different sections using divs with shared classes but unique IDs. Each div is set to take up 100% of the viewport height. To navigate between these sections, I want to provide Up/Down arrow buttons for users to click on.

I have written a JavaScript script for these navigation buttons. Upon reviewing it line by line, everything seems correct, but unfortunately, it's not functioning as intended :( Any assistance would be greatly appreciated!

<div id="1" class="slideContainer"> </div>
<div id="2" class="slideContainer"> </div>
<div id="3" class="slideContainer"> </div>

<div class="up" id="up">
    <img src="img/up.png">
</div>
<div class="down" id="down">
     <img src="img/down.png">
</div>


document.getElementById("up").addEventListener("click", goUp); 

function goUp() {
    var visibleId = $("div.slideContainer:visible").prev().attr('id');
    var targetID = visibleId;
    document.getElementById(targetID).scrollIntoView();

}

document.getElementById("down").addEventListener("click", goDown); 

function goDown() {
    var visibleId = $("div.slideContainer:visible").next().attr('id');
    var targetID = visibleId;
    document.getElementById(targetID).scrollIntoView();

}

Answer №1

If you prefer not to rely on any external libraries, consider using the following code snippet.

$('#up').on('click', function() {
    var mId = $(this).parent().siblings('.active');
    if (mId.attr('id') === '1') {
        return false;
    } else {
        mId.prev('.slideContainer').addClass('active');
        mId.removeClass('active');
    }
});
$('#down').on('click', function() {
    var mId = $(this).parent().siblings('.active');
    if (mId.attr('id') === '3') {
        return false;
    } else {
        mId.next('.slideContainer').addClass('active');
        mId.removeClass('active');
    }
});
* {padding: 0; margin: 0; box-sizing: border-box;}
.img-wrapper {
    position: relative;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}
.img-wrapper img {
    width: 100%;
    min-width: 100%;
    min-height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}
.img-wrapper > div:not(:first-child) {
    position: absolute;
    left: 0;
    height: 100%;
    width: 100%;
    transition: all ease 0.3s;
    overflow: hidden;
}
.img-wrapper div:nth-child(2) {
    z-index: 1;
    top: 0;
}
.img-wrapper div:nth-child(3) {
    z-index: 2;
    top: 100%;
}
.img-wrapper div:nth-child(4) {
    z-index: 3;
    top: 100%;
}
.img-wrapper div.active {
    top: 0;
}
.control {
    position: absolute;
    top: 15px;
    left: 10px;
    z-index: 10;
    display: flex;
}
.control div {
    padding: 6px 12px;
    background-color: green;
    color: yellow;
    margin: 0 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="img-wrapper">
    <div id="contr0l" class="control">
        <div class="up" id="up">
            Up
        </div>
        <div class="down" id="down">
             Down
        </div>
    </div>
    <div id="1" class="slideContainer active">
        <img src="https://picsum.photos/id/1018/1280/720" alt="">
    </div>
    <div id="2" class="slideContainer">
        <img src="https://picsum.photos/id/1019/1280/720" alt="">
    </div>
    <div id="3" class="slideContainer">
        <img src="https://picsum.photos/id/1020/1280/720" alt="">
    </div>
</div>

If you are open to using a library, I recommend checking out the fullpage plugin. It is very versatile and can be enhanced with animations from animate.css as well.

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

Identifying flex-wrap capabilities across different browsers

Currently, I am developing a project that involves implementing a responsive grid using the flex wrap property. However, since my project needs to support IE9 and older versions of Firefox (version 28 and below), I am in need of a way to determine their ...

Adjusting the panel dimensions based on the screen size

One feature on my website is a sliding panel (Image 1) located on the right side. It appears and disappears when a button is clicked, covering the entire height of the screen.https://i.sstatic.net/dF5Zc.jpg Here's the CSS code for this sliding panel- ...

Learn the process of transferring information from a dynamically generated table to a database using PHP

After creating a table using PHP dynamically, I am facing an issue with updating some cell values based on user input. I have provided my code below. I tried using [] in the names attribute to make names an array as suggested on Stack Overflow, but it didn ...

Is your Material UI Responsive Appbar overlapping the main content? Looking for a solution to address this issue?

Currently, I am in the process of developing a website that incorporates a responsive-based app bar with an attached drawer. The design concept I am following can be located at this link, which I have been modifying to suit my needs. However, I have encoun ...

How can I apply an underline effect when hovering over navigation links in a responsive menu?

I've successfully added an underline effect on hover for the navigation menu. However, I encountered an issue with the responsiveness of the menu. When viewed on screens smaller than 600px, the underline effect covers the entire width of the block ins ...

Tips for handling null input values when saving to a database

<!DOCTYPE html> <html> <head> <title>Data</title> </head> <body> /* Enter your data here */ <form action="this.php" method="post"> <input type='text&a ...

"Enhance your images with dynamic captions using jQuery

Struggling to create an image caption that only appears when hovering over the specific image? Not sure how to select the correct object? The caption is working, but it's appearing for all images at once. I have attempted to target a single image wit ...

Customize the position of the date picker for HTML native <input type="date">

I have implemented the HTML native input date element (found here- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date) and encountered an issue in Safari on macOS where my date picker is being displayed off-screen. How can I resolve this ...

Ways to create spacing between property columns in MUI grid fashion

Is there a way to achieve space-between alignment when elements are stacked vertically in MUI Grid instead of using flex? I've come across suggestions involving giving flex:1 and height:100%, but I'm specifically interested in achieving this with ...

How can I arrange the "elements" within a bootstrap container?

Check out the code display here: Here's what I aim for it to show The alignment of the elements has been a real challenge for me. I just can't seem to get the "Gender:", "Gender RadioButton list", "Age:", "Age RadioButton list" to appear in 4 c ...

Error code E11000 is indicating that a duplicate key issue has occurred in the collection "blog-api.blogs" where the index "id_1" is

Issue with Error E11000 duplicate key error collection: blog-api.blogs index: id_1 dup key: { id: null } Encountering an error when trying to insert data after initially inserting one successfully. Referencing the blogSchema.js: const mongoose = req ...

Issues with the proper functionality of the .ajax() method in Django

I'm currently facing an issue with my Ajax code while trying to interact with the database in order to edit model instances. I noticed that the first alert statement is functioning correctly, however, the subsequent alert statements are not working as ...

Styling CSS for HTML links

I am encountering an issue with a CSS hover effect on an image that is also a hyperlink to another website. The CSS styling for the hover effect seems to be interfering with the functionality of the HTML hyperlink. The image in question is aligned with ot ...

Div element positioned outside of another div element

I hate to bug you with more css issues, but I'm really struggling with this one. The problem I'm facing is that a sub div is overflowing the boundaries of its parent div. I tried using: display: inline-block;` but then the outer div goes haywir ...

Is it better to utilize a sizable JavaScript file or opt for a compact one?

I recently created a JavaScript file with over 6000 lines of code for various sections of my website. I'm debating whether to keep it as one large file or break it up into smaller parts and call them in their respective sections. What do you think? ...

Error: The function is not defined on this.props during the handleCHange event

After going through numerous answers to similar questions on this topic, I believe that I am following all the necessary steps but for some reason, it is not working. Below is the specific section of code that is causing the error: import React from &apos ...

Modifying HTML text with JavaScript according to the content of the currently selected div

Greetings! This is my first time posting a question, and I wanted to mention that I am relatively new to javascript/jquery. I have a gallery that displays image details on hover through text, while clicking on the image triggers a javascript function that ...

Empty space to the left of the vertical navigation bar

I'm struggling to get rid of the white space on the left side of my vertical navigation bar. I've attempted setting padding-left to 0 on my main navigation bar. This is my first time creating a navigation bar, so if you notice any semantic issu ...

jPlayer - Utilize the setMedia() function to define the media source URL

Having some trouble using jPlayer on Firefox 3.6 (Ubuntu) function loadmedia() { $('#jquery_jplayer_1').jPlayer('setMedia', { mp3: 'media/audio/04-Piste_4_1.mp3', }); } $(document).ready(function () { $('#jque ...

What is the best way to incorporate an image that appears when a user clicks on a

My goal is to dynamically place an image exactly where a user clicks on the webpage. Currently, I have the following code, but it only adds the image at the top and continues to do so repeatedly...not appearing at the clicked location. <html> ...