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

CSS is not referred to as animation

Every time we hit a key, the dinosaur receives a jump class that should activate an animation. Unfortunately, the animation does not play. animation not functioning <!DOCTYPE html> <html lang="en"> <head> <meta c ...

App builder shows raw HTML code instead of generating a dynamic table

I am currently working on an application that requires gathering data in a csv format from Google Sheets and importing it into App Inventor. Additionally, I need to include HTML code to display the table in a more visually appealing way. Here is what the ...

Using jQuery to load and parse a JSON file stored on a local system

I am a beginner in scripting languages and recently searched for ways to load and parse JSON files using jQuery. I found helpful resources on Stack Overflow. The JSON file I am working with is called new.json. { "a": [ {"name":"avc"}, ...

Any suggestions for a quicker method to manage state changes between OnMouseDown and OnMouseUp events in React?

I am attempting to create a window that can only be dragged from the top bar, similar to a GUI window. Currently, I have set up state updates based on OnMouseDown and OnMouseUp events on the top bar. However, I am experiencing slow updates as it seems to ...

The functionality of jQuery .rotate() appears to be malfunctioning

I'm struggling to get this code working properly. My jQuery version is 2.1.0. Here's the code snippet: $("a.shownav img").rotate(180); Any suggestions on how to make it function correctly without relying on a plugin? ...

Disable the background color css when hovering over a button

I'm having trouble with my Angular and HTML code. https://i.stack.imgur.com/Ea5oV.png The image above shows that a background color appears when hovering over the first icon. I've attempted: .sidemenuitm { padding: 10px 5px; cursor: poin ...

The (ReactJS + Redux) form fails to load when the /new segment is appended to the URL

I'm currently working on a project using React, Redux, and Rails. I've encountered an issue with loading the form. In my App.js file, I have set up a Router that defines the routes. import React, { Component } from 'react'; import { Br ...

Getting a null value for active user after completing the sign-in process

I am using local storage to store username and password. However, I am encountering an issue where the active user is returning null after a certain line of code, and I am unsure why. console.log("I am the Active user: " + activeUser); const me ...

absence of data returned in Ajax call for a REST endpoint

Currently, I am testing a straightforward REST service that was created using Java. The server-side code includes: import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import ...

What is the best technique for positioning a div using the ELEMENT_NODE method?

  #top { height: .5rem; padding-left: 2.7rem; line-height: .5rem; color: #666; font-size: .12rem; position: relative; background-color: rgb(241, 241, 241); } #top div { position: ...

organize divs in a nested angular style

Looking to showcase div elements in a specific layout within my Angular project: The current HTML structure is as follows: <div class="outer-wrapper"> <div class="image" ng-repeat="image in images" ng-if="showImages" ng-click="imageClick(ima ...

Exploring anchor elements within a div using Selenium in Python to extract URLs

Hey there, I'm currently attempting to iterate through anchor elements within a div and retrieve the links of these elements. <div class="List"> <a class="selected" href="link">text 1</a> <a ...

Admin template system for CodeIgniter

In order to give the Administrator the ability to customize the Admin Dashboard Layout and provide an option for them to upload their own layouts or "Premium" layouts, I need to implement a solution. I have considered one approach: Utilizing CSS (allowin ...

The method to obtain a result array using the getJson function in CodeIgniter

Here is the code snippet I am working with: function retrieveAllProcessingTransactions() { $resultSet = $this->db->query("SELECT a.id_transaksi, ETC"); return $resultSet; } In my controller file: public function fetchTransac ...

Adjusting div height as you scroll down the page

Hello, I am new to CSS and I have encountered an issue with setting the height of a div element to 100%. When the list on my page exceeds the size of the page, the div is no longer visible when scrolling. Do you have any ideas on how to fix this? Here is t ...

Cypress - simulate multiple responses for watchPosition stub

I have a small VueJs app where clicking on a button triggers the watchPosition function from the Geolocation API to retrieve the user's position and perform some calculations with it. Now, I want to test this functionality using Cypress testing. To ...

Enhance the appearance of the input range by styling it with an additional class both before and

I have a JSF page where I am using an input range element. I want to add a different style class before and after the input range. Specifically, I want to display a plus icon (ui-icon-plusthick) before the input and a minus icon (ui-icon-minus) after the i ...

jQuery ajax functions only function properly in Internet Explorer

Having an issue with my multilingual MVC 3 application. I am using a dropdownlist to switch languages by saving the value into a cookie and session. The problem is that it only works in IE. When I reload the site or close Firefox or Chrome, the language ch ...

Variations in the appearance of scrollbars on the x-axis and y-axis within a single <div> element

Let's say we have a div element like this: <div class="nav-menu"></div> I want to customize the CSS style for scrollbar using the ::scrollbar pseudo-element: .nav-menu::-webkit-scrollbar { width: 8px; background: white; ...

How can I add divs to an HTML page with a Javascript animated background?

I am currently facing an issue with my JavaScript animated background, consisting of just 3 pictures. I am trying to display some Div elements on it with content inside. Here is the code snippet I have right now: In my CSS file, I have defined styles for ...