Is there a way to horizontally navigate a pallet using Next and Prev buttons?

As someone new to development, I am looking for a way to scroll my question pallet up and down based on the question number when I click next and previous buttons.

In my pallet div, there are over 200 questions which are dynamically generated. However, there may be instances where there are fewer than 200 questions.

Here is a snippet of my code:

Within my next button, I have the following function written:

function next(id) {

if (id >= 40 && id < 79) {
    $(".question-container").animate({scrollTop: 370}, 'fast');
}
if (id > 80 && id < 119) {
    $(".question-container").animate({scrollTop: 740}, 'fast');
}
if (id > 120 && id < 159) {
    $(".question-container").animate({scrollTop: 1110}, 'slow');
}
if (id >= 160 && id < 200) {
    $(".question-container").animate({scrollTop: 1480}, 'slow');
}
if (id >= 200 && id < 240) {
    $(".question-container").animate({scrollTop: 1850}, 'slow');
}
if (id >= 240 && id < 300) {
    $(".question-container").animate({scrollTop: 2220}, 'slow');
}

Can anyone provide assistance with this?

Thank you, Thangavel

Answer №1

If you're looking for a quick fix, one solution could be to utilize the window.scrollTo() method after determining the top position of your desired element.

function scrollToElement(item) {
  var topPosition = document.getElementById(item).offsetTop;
  window.scrollTo(0, topPosition);
}
p {
  height: 800px;
}
<button onclick="scrollToElement('one')">Scroll to element</button>
<p></p>
<div id="one">This is the target element</div>

Alternatively, you could consider using a jQuery plugin like "carousel". There are numerous options available online, but if you want to try out my version, it's accessible here: https://github.com/gibbok/jquery-slidertv

Answer №2

Utilize the following code snippet:

 window.scrollBy(0, 10);

in your JavaScript to initiate a downward scroll of 10 pixels. Feel free to modify the value to adjust the scrolling distance as needed. For further information, visit this link

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

What are the steps to successfully implement "Pointermove" event delegation in Safari iOS for parent/child elements?

I've encountered an issue that I'm struggling to find a solution for. My goal is to implement an event delegate using pointermove on a parent container, and I need to be able to detect when the event transitions between a child element and the pa ...

An unexpected import token was encountered while using ReactJS and Babel

Every time I attempt to launch my application, an error message pops up that says: (function (exports, require, module, __filename, __dirname) { import { Row } from '../grid' SyntaxError: Unexpected token import I've experimented with vari ...

Steps for updating text within an object in Angular

details = [ { event: "02/01/2019 - [Juan] - D - [Leo]", point: 72 }, { event: "02/01/2019 - [Carlo] - N - [Trish]", point: 92 } ]; I am attempting to modify the text within the titles that contain - N - or - D - The desired outcom ...

Uploading a Node.js Package to GitHub Packages - Issue ENEEDAUTH

Hello everyone, I am currently attempting to deploy my NPM package to GitHub packages using the following yaml configuration: # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created # For m ...

What exactly does the <cbn-root> HTML element signify, and how can it be parsed using Java programming language?

I attempted to develop a Java program to monitor the availability of reserved spots on this website: However, when I examine the page source using Chrome or Edge, only <cbn-root></cbn-root> is displayed in the body section. Yet, utilizing Chro ...

Tips for retrieving the count from HTML using JavaScript:

I need to determine the count of list items in an unordered list within dir-pagination-controls. How can I achieve this using JavaScript? <dir-pagination-controls min-size="1" direction-links="true" boundary-links="true" class="pull-right ng-isolate- ...

Instructions on passing a PHP variable as a parameter to a JavaScript function when a button is clicked

In my implementation of codeigniter 3, I have a view page where the data from $row->poll_question is fetched from the database. The different values within this variable are displayed on the voting.php page. Next to this display, there is a button labeled ...

Exploring the Mathematical Capabilities of jQuery

I have a question regarding a jQuery code that retrieves the price of Bitcoin from a JSON API and displays its current value in euros. I am interested in converting this to another currency, such as Danish Crown or any other. Essentially, my query is whe ...

Issue with :hover pseudo-class in IE8

If you're unsure about my explanation, check out the video for a visual demonstration. Pay close attention to the cursor movements while I attempt to optimize my website for IE8. Hopefully there is a solution to this issue. Thank you in advance! http ...

Tips for making a Bootstrap dropdown submenu appear as a 'dropup'

I'm using a Bootstrap dropdown menu with a submenu that should drop upwards while the rest of the menu drops down. How can I achieve this? Here is the code snippet: <div class="dropdown"> <a class="inputBarA dropdown-toggle" data-toggle= ...

Excessive image display | HTML and CSS synergize

I am having some trouble with my image gallery. Each image is displayed as a vertical column and has zooming effects when hovered over. Clicking on an image should show it in full screen with a caption. The problem arises when dealing with images that are ...

Tips for utilizing the form.checkValidity() method in HTML:

While delving into the source code of a website utilizing MVC architecture, I encountered some difficulties comprehending it fully. Here is a snippet of the view's code: function submitForm (action) { var forms = document.getElementById('form& ...

Using Laravel with Ajax can sometimes result in successful requests, while other times they may fail

I am facing some issues with my code as a junior programmer. Sometimes when I call data using Ajax in laravel 5.7, it is successful and the data is retrieved. However, upon refreshing, sometimes it is incomplete and logs show an error 500 in my PHP. Strang ...

How to use Angular 2 to communicate with JavaScript API whenever the router switches to

I am currently working on an Angular2 component that has a template which relies on JavaScript calls to load various components such as Facebook, Google Maps, and custom scripts. The necessary scripts are already loaded in the index.html file, so all I ne ...

Here's how you can combine two individual LESS files using grunt-recess

Hey there! I'm working on compiling 2 separate files using grunt recess: recess: { dist: { options: { compile: true }, files: { 'path/css/custom. ...

Retrieve Header information when making a cross-domain AJAX request

I've been working on a way to validate image URLs by using an AJAX call to check if the image is still available. The challenge I'm facing is that the image server is on a different domain, so I included the crossDomain: true attribute in my AJAX ...

The $http patch request is failing to transmit data to the server

Recently, I started utilizing AngularJS to create a CRUD functionality using Laravel and AngularJS. I have implemented a function that serves the purpose of both updating and saving data. Here is my function code: $scope.save = function (modalstate, id) ...

Should I install both dependencies for Moment.js Plugins?

Thinking about incorporating a moment.js plugin such as moment-business-days. Do I need to install both packages? npm install moment-business-days npm install moment // yes / no / maybe? Or is it sufficient to just install the plugin since it already inc ...

"An in-depth guide on parsing JSON and showcasing it in an HTML format

As part of my order processing, I am saving the order details into a JSON file named order_details.json. Here is an example of how the data is structured: [{ "uniqueID": "CHECKOUT_IE01", "orderID": "4001820182", "date": "06-02-2019 16:55:32.32 ...

new cookies are created without human intervention

Working on a project in Next.js and using the react-cookie npm package for managing sessions. I am creating a cookie with the key name "token" and assigning the token value as its value. However, an issue has arisen related to cookies where multiple cooki ...