Error encountered while scrolling with a fixed position

I am currently in the process of developing a carousel slider that resembles what we see on Android devices. The main challenge I am facing at this early stage is applying the CSS property position: fixed; only horizontally, as vertical scrolling will be needed.

Is there a way to limit the application of position: fixed; to horizontal scrolling only?

Below is the snippet of code I am working with:

var val = 0;
$("button").click(function() {
  for (var i = 0; i < 9; i++) {
    $('.MySlide:nth-child(' + (i + 1) + ')').removeClass('MySlide' + (((val + i) % 9) + 1) + '');
    $('.MySlide:nth-child(' + (i + 1) + ')').addClass('MySlide' + (((val + i + 1) % 9) + 1) + '');
  }
  val = (val + 1) % 9;
});
.long-div {
  height: 300px;
}
.MySlides-wrap {
  border: 1px;
  border-style: solid;
  height: 350px;
}
/* MySlide styles go here */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Slider Test</title>
  </head>
  <body>
    <div class="long-div"></div>
    <div class="MySlides-wrap">
      <div class="card MySlide MySlide1"></div>
      <div class="card MySlide MySlide2"></div>
      <div class="card MySlide MySlide3"></div>
      <div class="card MySlide MySlide4"></div>
      <div class="card MySlide MySlide5"></div>
      <div class="card MySlide MySlide6"></div>
      <div class="card MySlide MySlide7"></div>
      <div class="card MySlide MySlide8"></div>
      <div class="card MySlide MySlide9"></div>
    </div>
    <button>Next</button>
  </body>
</html>

Answer №1

There are multiple ways to address this issue, however fixed positioning is commonly used to affix an element to a specific location on the screen rather than within the webpage itself. One suggestion would be to create a scrollable container and manage the scroll position using jQuery.

Here is a functional example (tested in Chrome and Firefox):

https://jsfiddle.net/7duatxmv/

Please excuse the inline styles used for demonstration purposes:

<div id="slideContainer" style="position:relative; overflow:hidden; white-space: nowrap;">
    <div class="slide" style="display:inline-block;">
        Content 1
    </div>
    <div class="slide selected" style="display:inline-block;">
        Content 2 (this slide is scrolled to by default)
    </div>
    <div class="slide" style="display:inline-block;">
        Content 3
    </div>
</div>

In this setup, the "slideContainer" would have a width matching that of a single slide, enabling navigation between slides using the browser's native scrolling capabilities.

jQuery can be used to control the scrolling using .scrollLeft() and .offset().

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

Update the href attribute with values from the form fields

My current project involves creating a form with 5 input fields: Service type Number of days Number of children Number of adults I want to dynamically change the value of a button based on these variables using JavaScript. Here is my code so far: JS ...

Unable to display Vue image within v-for loop

I'm facing an issue with rendering images from an array of objects. Even though the paths to the images are correct, the images are not displaying. I've been following a tutorial at which suggests specifying the image URLs as links. I've t ...

How can one retrieve the 'alt' attribute text from an image tag on a webpage using the JSOUP library in an Android application?

Extracting the 'alt' text from an 'img' tag in Android after clicking a button is causing issues. The attempted code is not working as expected. Here is the snippet of the code that was used: Document doc = Jsoup.connect(url).get(); ...

EJS is having trouble locating an image within the static public directory

I'm currently facing an issue with rendering a HTML template as a PDF using EJS. Despite my efforts, the images are not displaying properly. Here is how my project is structured: - public - images image.jpg - routes documentsRoute.js ...

Unable to retrieve input using jquery selector

I am attempting to detect the click event on a checkbox. The Xpath for the element provided by firebug is as follows, starting with a table tag in my JSP (i.e. the table is within a div). /html/body/div[1]/div/div/div[2]/div/div[2]/div[1]/div/div[2]/table ...

Typescript - Error in Parsing: Expecting an expression

I am currently working with Vue and TypeScript and have encountered a problem. How can I resolve it? Here is the code snippet in question: private setTitle(systemConfig: any) { const systemConfigParse; let obj; systemConfigParse = JSON.parse(sy ...

Problem encountered when attempting to return data received from database queries executed within a loop

Having an issue with making multiple MongoDB queries in a loop and trying to send all the results as one data array. However, simply using 'return' to send the data is resulting in 'undefined' and not waiting for the results of all DB r ...

AngularJS encounters a lack of 'Access-Control-Allow-Origin' header

I have encountered an issue with my AngularJS application. I am attempting to send data to a third-party URL for storage on their server. However, when I execute the code below, I receive the following error message: XMLHttpRequest cannot load . Response t ...

unable to update the table due to issues with the knockout observableArray

My goal is to collect values from form fields and store them as an object in an observableArray. I want to display these objects in a table so that every time I hit the 'add' button, the table should be updated. However, I am facing issues with t ...

Do not procrastinate when updating the navbar elements while navigating through pages

This specific NextJS code is designed to alter the color of the Navbar elements once scrolling reaches 950px from the top or when navigating to a different page that includes the Navbar. Strangely, there seems to be a delay in updating the Navbar colors wh ...

I am unable to select the first item when using select2.js and setting a placeholder

I am experiencing an issue with my <select> list that is styled using select2.js. Everything seems to be functioning properly, except for when a placeholder is used. Users are unable to select the first item in the list initially. If they choose any ...

I'm having trouble getting a http.request to run within an imported module. It just doesn't seem to work when I try to access

Here is the code from my main.js file: var spaceJson = require('./space.js'); var options = { "method": "GET", "hostname": "10.10.111.226", "port": null, "path": "/API/Org", "headers": { "authorization": "Bearer eyJ0eXAiOiJKV1QiLCJ ...

Divergent Appearance of Input Field in Chrome versus Firefox

After testing in Chrome and Firefox, I noticed that the input box appears larger than expected in Firefox. Below is the HTML markup: <div class="form-wrapper"> <input type="search" name="Ofsearch" placeholder="Search h ...

Running multiple controller functions in nodejs can be achieved by chaining them together in the desired

Currently, I am working on the boilerplate code of mean.io and adding a password reset email feature to it. Whenever a user requests a password reset with their email as a parameter, I generate a unique salt (resetid) and send them an email with a link con ...

JavaScript is unable to identify the operating system that is running underneath

Even though I am on a Windows system, the browser console is showing that I am using Linux. function detectOS() { const userAgent = navigator.userAgent.toLowerCase(); if (userAgent.includes('win')) { return 'Windows' ...

How to programmatically close a colorbox using an ASP.NET form's Submit button within an iframe

I've looked at other similar questions, but I just can't seem to make this work. I want to close the colorbox iframe when a .NET Button form within the iframe is clicked. <script type="text/javascript"> $(document).ready(function() { ...

Using JQuery to monitor the loading of a newly selected image src attribute

As a newcomer to JavaScript and JQuery, I am facing a challenge that I couldn't find a solution for in other discussions: I have a function that retrieves the path to an image (/API/currentImage) using a GET request and needs to update the src attrib ...

'view' is not recognized as a valid property of the 'div' element in Ionic3, so it cannot be bound

I am a beginner with Ionic and I'm trying to incorporate Angular Calendar into my Ionic3 application. However, I am encountering an error that says, "Can't bind to 'view' since it isn't a known property of 'div'. Here&ap ...

How can this JSON function correctly?

Hey there! I've been trying really hard to receive a response from PHP and based on that response, perform different actions. However, I seem to be facing some challenges with handling the output from the PHP file. Below is a snippet of my index file: ...

Trigger the useEffect Hook once the condition has been satisfied

Within my component, I am utilizing the useEffect hook to monitor updates of a specific value. I have implemented a condition to prevent the useEffect function from triggering after the initial rendering. However, I noticed that each time the button is c ...