The scroll function triggers even upon the initial loading of the page

Trying to solve the challenge of creating a fullscreen slider similar to the one described in this question, I created a jsfiddle (currently on hold)
Despite knowing that scrolling too fast causes bugs and that scrolling both ways has the same effect, my main issue is that upon page load, it automatically scrolls once.
The code snippet utilized for this attempt:

var leftImg = document.getElementsByClassName('left');
var rightImg = document.getElementsByClassName('right');
var cur = 0;
for (i=0; i<3; i++){
    leftImg[i].style.zIndex = rightImg[i].style.zIndex = -(i+1);
}

window.onmousewheel = changeImage();

function changeImage() {
  leftImg[cur].style.top= "-100%";
  rightImg[cur].style.top= "100%";
  setTimeout(function(){
    window.onmousewheel = changeImage; 
  leftImg[cur].style.zIndex=rightImg[cur].style.zIndex=-(cur+4);
  leftImg[cur].style.top=rightImg[cur].style.top="0";
  cur++;
  if(cur === 3) {
    cur = 0;
for (i=0; i<3; i++){
    leftImg[i].style.zIndex = rightImg[i].style.zIndex = -(i+1);
}
  }
}, 3000);
  window.onmousewheel = preventDefault;
}  

The persistent question is: Why does this behavior occur?

Answer №1

MODIFIED #2

No requirement for the loop or TimeOut

var cur = 2;
var zIndex = -1;

window.onmousewheel = alterImage;
function alterImage() {

    var leftImg = document.getElementsByClassName('left');
    var rightImg = document.getElementsByClassName('right');
    leftImg[cur].style.top = "-100%";
    rightImg[cur].style.top = "100%";
    if (leftImg[(cur + 1) % 3].style.top === "-100%") {
        leftImg[(cur + 1) % 3].style.zIndex = rightImg[(cur + 1) % 3].style.zIndex = (zIndex);
        leftImg[(cur + 1) % 3].style.top = rightImg[(cur + 1) % 3].style.top = "0%";

    }
    zIndex--;
    cur--;
    if (cur === -1) {
        cur = 2;
    }

}

View example

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

Implementing pagination in Webgrid using AJAX post method

I've developed this JavaScript code: function PartialViewLoad() { $.ajaxSetup({ cache: false }); $.ajax({ url: "/ControllerAlpha/MethodBeta", type: "GET", dataType: "html", data: { s ...

What is the best way to integrate a Ruby object into JavaScript?

I'm attempting to integrate Ruby into the JS.erb file, where I need access to the @user object and its relationships. Instead of converting to a JSON object, I prefer the ERB to precompile on the server-side. My goal is to iterate in the JS file rat ...

The Challenge of Implementing Jquery in Internet Explorer

Having troubles with Jquery in IE, the code is not working. Can anyone assist me? Please check this on IE and FF (or opera, safari, chrome), select a state, and you'll notice that clubs load but do not appear in IE while they show up in FF. Your hel ...

Display a full-screen image and align it horizontally from the center using CSS styling

When I fill a screen with an image using the following code: width:auto; height:100%; The image fills the screen vertically and almost entirely horizontally, but it gets cropped off on the right side. Is there a way to make the image float centered on th ...

How can I make two flexbox items in a row stack on top of each other as a column?

I'm currently working on a layout that involves a parent flexbox containing multiple sections. My challenge lies in getting two specific sections to stack vertically while the rest of the layout remains horizontal. At the moment, I have two sections ...

Creating a custom comparison method between two select dropdowns using the jQuery Validation plugin

In my HTML code, I have two select elements: <label for="h_slat_type">Horizontal Slat Type</label> <select name="h_slat_type" id="h_slat_type"> <option disabled="disabled" selected>Select</option> ...

`AJAX jQuery for efficient file uploads`

I am struggling to upload a file input using jQuery ajax without causing the page to refresh. Here is my HTML form: <form name="uploadform" id="uploadform" method="post" enctype="multipart/form-data"> <div name="profileBiodata" id="profileBioda ...

JavaScript: Receiving an error that function is undefined when working with data binding

Why is my code showing that it's not defined while I'm attempting a simple code with data binding? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="ht ...

What are the steps for populating a table cell with data using AJAX, MySQL, and PHP in the given situation?

The challenge is to create an HTML table with two columns where the first column gets its data from PHP and MySQL. The goal is to turn the values in the first column into clickable links that, when clicked, trigger an AJAX call to fetch information from th ...

How can I generate a fresh window/frame within my Javascript canvas?

Is there a way to open a new frame or window within the canvas using JavaScript? I am working on a game project where I want to implement a feature that displays additional information on a menu screen when a button is pressed. Currently, I have managed to ...

Prevent selection of future dates in HTML input using PHP exclusively

I am looking for a way to limit the date input on my form using PHP and HTML. <?php echo '<input type="date" name="doc" value="' . date("d-m-Y") . '" min="11-05-2014" max="' . date("d-m-Y") . '"/>'; ?> While rese ...

What is the significance of the exclamation point before and after in JavaScript?

Currently collaborating on a project and attempting to decipher the significance of the exclamation marks both before and after. import ICHING from '!json!constants/iching_deoxy.json'; ...

"Layering text over images using background colors for a unique design touch

Is there a way to create a solid-color overlay that perfectly matches the size of an image using just HTML and CSS? I want to display text on this overlay, but it needs to work with images of any size. The image will be centered and resized to fit within ...

Accessing the phone number input from a form in Rails and verifying its presence in the database. Implementing distinct ajax responses based on the existence of the value

Looking for a way to verify phone numbers? The process involves users entering a phone number, submitting the form, and cross-referencing it with a database of phone number records to determine if it's valid. My goal is to provide different AJAX respo ...

What is the best way to appropriately update a partial that displays another partial (refreshing with JavaScript leads to a page within a page)?

Update 2 - marzapower's assistance proved valuable once again. The success section in the ajax was identified as the root cause of the issue, as explained below. UPDATE: marzapower's solution works effectively (refer to it below), but I require ...

Issue with Laravel: Using `$request->all()` results in an empty array when called using JSON XHR

Having trouble using $.ajax and only the XMLHttpRequest for sending JSON to a Laravel controller. Keep getting 500 errors when attempting to make the request. Here's the method I'm using to send the data: const sendEdit = function(){ ...

Struggling to perfectly align a Wufoo form within an iframe using CSS

I have integrated a web form from Wufoo into my webpage using an iframe. Custom CSS can be used to style the form and I am trying to center it within the iframe. Below is the HTML code of the form displayed in the iframe: <div id="container" class="lt ...

Acquiring JSON data through the use of jquery and php

Below is the HTML code I am working with: <div id="myDiv"> <p class="readMore"></p> <p class="readMore"></p> <p class="readMore"></p> </div> This is the jQuery function I am using: UPDATE <script> ...

Could there be an issue with my function parameters, or is there another underlying problem?

Hey there! I've been working on this function, but it doesn't seem to be running quite right. Here's the code: function chooseCols(colTag, tagName) { // Set column name var column = $('.tagChooser:eq(&apo ...

How can I make the arrows work on a secondary slider with EasySlider1.7?

I finally managed to get 2 sliders working on my page after reading through several other tutorials. However, I am facing an issue with the Prev & Next arrows on the second slider as they don't seem to work properly. I inherited this page from someon ...