Tap to navigate to queued sections on click

Greetings, community members! Despite my extensive research, I have been unable to find a solution to my query.

Just a heads up - I am a novice when it comes to javascript and jQuery.

My question pertains to incorporating two image buttons that, upon clicking, will scroll to the next paragraph or div element. The images in question are designed to resemble buttons.

To give you an idea of what I am aiming for, here is a snippet of the HTML code:

<div id="scrollbuttons">
    <img id="prev" src="pics/prev.png"><a href="#"></a></img>
    <img id="next" src="pics/next.png"><a href="#"></a></img>
</div>

Additionally:

<div id="work">
    <p class="maintext">blabla</p>
</div>
<div id="gallery">
    <p class="maintext">blabla</p>
</div>
<div id="project">
    <p class="maintext">blabla</p>
</div>
<div id="finish">
    <p class="maintext">blabla</p>
</div>

The goal is to enable smooth auto-scrolling so that upon clicking "next", the page smoothly transitions to the "work" section, followed by "gallery" and so forth. Similarly, pressing "prev" should smoothly move back to the previous point.

I am working with the latest version of jQuery and would prefer not to rely on plugins unless absolutely necessary.

I hope this detailed explanation provides enough context for assistance. Being new to JS, any guidance would be greatly appreciated. Thank you in advance.

Sincerely, Emil Nilsson

Answer №1

If you're looking for a more efficient way to navigate through sections on a webpage, check out this dynamic solution. Simply click the buttons to move forward or backward. A class called section has been added to all content divs for easy identification.

Check out the code in action on JSFIDDLE


var currentSection = "start";

$("#next").click(function(){

    if(currentSection == "start"){
       var nextSection ="work";
    }else{
        var nextSection = $("#"+currentSection).next(".section").attr("id");
    }

      $("html, body").animate({scrollTop: $("#"+nextSection).offset().top});
      currentSection = nextSection;
  });

$("#prev").click(function(){

   var prevSection = $("#"+currentSection).prev(".section").attr("id");

  $("html, body").animate({scrollTop: $("#"+prevSection).offset().top});
    currentSection = prevSection;
 });

Answer №2

perhaps you could consider using a solution similar to this one

http://jsfiddle.net/azerty/

placing the image inside of the anchor element for better semantics and W3C validation:

<a href="#"><img id="next" src="pics/next.png"></img></a>

in my approach, all you need to do is specify the initial element and the div's ID

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

The max-width attribute is failing to apply to the form within the Bootstrap framework

login.html file: {% extends "base.html" %} {% block content %} <div class="container login-page-form"> <form id="login-form" action="/auth/login" method="post"> <div class="f ...

Tips for extracting information from JSON data in the provided API example

{"Dina-Kar":{"com":"available"},"DinaKarPool":{"com":"available"},"DinaKarStore":{"com":"available"},"DinaKarOnline":{"com":"available"},"DinaKarParts":{"com":"available"},"DinaKarJack":{"com":"available"},"DinaKarTunes":{"com":"available"},"DinaKarSmart": ...

Having trouble with the Material-UI v1 Drawer component on the iOS 13 beta version

As we prepare for the upcoming iOS release, set to debut next month, it has come to our attention that the main navigation on our website is experiencing issues when accessed using an iOS device running the latest beta (iOS13). While the drawer opens as ex ...

Issues with maintaining the checked state of radio buttons in an Angular 4 application with Bootstrap 4

In my Angular 4 reactive form, I am struggling with the following code: <div class="btn-group" data-toggle="buttons"> <label class="btn btn-primary" *ngFor="let item of list;let i=index" > <input type="radio" name="som ...

Troubleshooting Problems with Angular ng-include

I'm having trouble with including a file using ng-include. I've tried various variations, but it keeps returning a 404 error in the console. The directory structure is as follows: -App --Layout ---Partials ----Navigation.tpl.html(File) --Layout. ...

Leveraging Toaster Notifications in AngularJS for Custom Exception Management and Logging

Implemented the use of AngularJS Toaster for effective notification handling. To customize exception handling, set up in index.html as shown below <toaster-container toaster-options="{'time-out': 3000, 'position-class': 'toast ...

Are there ways to incorporate a variable within a Regular Expression in Javascript?

I've already checked the Mozilla website and W3schools, but I can't seem to find the solution. var modifyString = function (string1, string2) { if (string2.match(string1)) { string1 = new RegExp(string1); string2 = string2.replac ...

What is the best way to denote arrays in ember-data models?

When dealing with an array in a model, is it essential to use DS.hasMany pointing to a DS.Model, even if the array elements are not actual models with IDs or endpoints? Is there a more efficient alternative? Despite using DS.hasMany with { embedded: true ...

What is the best way to link HTML transformations across several classes?

Is it possible to combine multiple transforms in a single element to create a unique end result? It seems that when applying multiple transform properties to an element, only one of them will be recognized. For example, trying to transform a div with bot ...

Steps for displaying a customized view within an NPM module:

Given that pushAsset prohibits loading external resources, I am interested in displaying this template/script from my NPM module: views/tag.html <script async src="https://www.googletagmanager.com/gtag/js?id={{ data.gid }}"></script> NPM mod ...

Is client-side JavaScript executed prior to server-side JavaScript in AJAX?

I'm curious about how client-side code interacts with server-side responses. I have a lengthy and complex piece of code that triggers a function which in turn executes some server-side code using a HTTPRequest Object, returning a string to the calling ...

Implement a custom placeholder feature for InputNumber in Blazor

I have a question about adding a placeholder to the InputNumber component. I attempted the following code but unfortunately, it did not work as expected. //Code behind public int? Hour { get; set; } //razor page <EditForm Model=&quo ...

Enhancing interactivity: Implementing a ripple effect for Card clicks in Material UI

I'm curious if there's a method to incorporate a ripple effect into Material UI's Card component when it is clicked. Additionally, I am wondering if it is achievable to have the ripple effect display on top of the content within the Card co ...

The symbol '★' represents the '★' content in CSS

Here is the CSS code I am using: .rating:not(:checked) > label:before { content: '★'; } However, when it is displayed on the screen, instead of seeing a ★, I see â˜. It's strange because if I use Firebug and manually change the ...

Implementing initial state checks for Alpine.js checkboxes based on x-modal is not functioning properly

Upon loading alpinejs, for some reason all checkboxes become unchecked. It's perplexing and I can't figure out why. <div x-data="{ colors: [orange] }"> <input type="checkbox" value="red" x-model="co ...

Visit the embedded AJAX feature that is failing to show any results

I've been working on this seemingly simple problem for days now, but I'm still stuck. I have the following Go code... package main import ( "fmt" "net/http" "html/template" "database/sql" _"github.com/mattn/go-sqlite3" ...

The image is failing to display in the CSS

As a beginner in the world of CSS and HTML, I encountered an issue where my background image is not showing up. Here's the code snippet that I have: ... <style type="text/css"> #header_contain{ width:935px; height: 135px; ...

Using the Match() function with an array of objects

I am working with an object array containing multiple variables. let Novels = []; class Novel { constructor(isbn, title, author, edition, publication, year) { this.isbn = isbn; this.title = title; this.author = author; this.publicat ...

Ways to determine if a browser is currently loading a fresh webpage

I'm experiencing an issue with my web app where the 'safety' code triggers a page reload if the server (Socket.IO) connection becomes silent for more than 5 seconds, often due to customer site firewall or broken-proxy issues. Although the S ...

The response parser in Angular 7 is failing to function correctly

Hey, I recently updated my Angular from version 4.4 to the latest 7 and after encountering several errors, I was able to get my service up and running. However, I'm facing an issue with my output parser function which is supposed to parse the login re ...