Problems Arising with Javascript Animation Functionality

I've created a script for an interactive "reel" that moves up or down when clicking on specific arrow buttons. However, I'm encountering two issues:

1) The up arrow causes it to move downward, while the down arrow moves it upward.
2) After executing once, I'm unable to trigger it again as an error occurs stating it couldn't parse "top".

Any insights on why this might be occurring? I've included all the code below, even though it's not the most organized.

<!doctype html>

<html>

<head>
  <meta charset="utf-8">
  <title>Tales of the Hartland: Homepage</title>

<style type="text/css">

/* ID DEFINITIONS */

#reelHolder {
    height: 750px;
    width: 800px;
    overflow: hidden;
    border: 1px solid gray;
    position: relative;
    left:50%;
    margin-left:-400px;
}

#arrowDown {
    z-index:auto;
    position:relative;
    left:87.4%;
}

#arrowUp {
    z-index:auto;
    position:relative;
}

#reel {
    position: relative;
}

/* CLASS DEFINITIONS */

.banner {
    position: relative;
    left:50%;
    margin-left:-600px;
}

.arrow {
    width:50px;
    height:50px;
}

.reeler {
    width: 600px;
    height:300px;
    position: relative;
    left:50%;
    margin-left:-300px;
    margin-top:50px;
}

.break {
    height:0px;
}

.bigBreak {
    height:50px;
}

</style>


<script>
// REEL UP
function reelUp(elem) {

elem = document.getElementById(elem);

var down = 0;
var curLoc = elem.style.top;

function frame() {

down++;

elem.style.top = down + curLoc + 'px';

if (down == 300)
clearInterval(id);
}

var id = setInterval(frame, 10); // draw every 10ms

}

// REEL DOWN
function reelDown(elem) {

elem = document.getElementById(elem);

var down = 0;
var curLoc = elem.style.top;

function frame() {

down++;

elem.style.top = (-1 * down) + curLoc + 'px';

if (down == 300)
clearInterval(id);
}

var id = setInterval(frame, 10); // draw every 10ms

}
</script>

</head>

<body>

  <img src="Banner.png" width="1200" height="150" alt="" class="banner"/>

    <a id="arrowUp" class="arrow" href="javascript:reelUp('reel')">
        <img src="arrowUp.png" alt=""/>
    </a>

    <a id="arrowDown" class="arrow" href="javascript:reelDown('reel')">
        <img src="arrowDown.png" alt=""/>
    </a>

  <div id="reelHolder" class="centerDiv">

    <div id="reel">
        <img src="reel1.png" class="reeler" id="reel1"/>
        <div class="break"></div>
        <img src="reel2.png" class="reeler" id="reel2"/>
    </div>

  </div>

</body>
</html>

Answer №1

Increasing the value of elem.style.top will cause the element to move further down within its container. For example, if top is set to 10px, the top of the element will be positioned 10 pixels from the top of the container. On the other hand, if top is set to 100px, then it will be located 90 pixels lower in relation to its previous position.

To move an element upwards in the reelUp section, you should decrease the value of top:

elem.style.top = curLoc - down + 'px';

Conversely, to shift the element downwards in the reelDown section, you need to increase the value of top:

elem.style.top = curLoc + down + 'px';

It seems that the reason your code only works once might be due to the variable id not being defined within the scope of the frame function. By declaring var id before the function frame(), this issue should be resolved.

These are the solutions to your specific inquiries. Once those have been addressed, I recommend consolidating the functions reelUp and reelDown into a single function since they share many similarities (with the only distinction being a single character: + or -).

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

Generating a highchart by retrieving JSON data using AJAX

I'm currently working on generating a basic chart on a webpage using data from a MySQL database that is fetched via a MySQL script. My main challenge lies in understanding how to combine the ajax call with the necessary data for the chart. I'm n ...

Angular Controller encounters issue with event handler functionality ceasing

One of my Angular controllers has the following line. The event handler works when the initial page loads, but it stops working when navigating to a different item with the same controller and template. document.getElementById('item-details-v ...

Oops, it seems like there is a TypeError with the function window.initMap in Google Maps

For the past week, I have been struggling to update my marks on Google Maps while using AJAX in an HTML page. My controller fetches data from the database and sends it back, but now I am encountering the following error: TypeError: window.initMap is not a ...

"Error: The function $(...).autocomplete is not recognized" in conjunction with Oracle Apex

Currently experiencing some frustration trying to solve the issue at hand. The Uncaught TypeError: $(...).autocomplete is not a function error keeps popping up and I have exhausted all resources on Stack Overflow in an attempt to fix it. This problem is oc ...

Incorporating an Edit button with an icon into a react-bootstrap-table2

Is there a way to insert buttons in the Edit column of my table so I can easily edit a row? I believe there should be a method to personalize the column and incorporate icons as shown in the example image. Sample Image of What I want to do: import React ...

Arrange a collection of objects by two criteria: the end time, followed by the status in accordance with the specified array order if the end times are equal

Is this the best method to arrange data by using infinity? I gave it a try but it doesn't quite meet my requirements. data = [{ "status": "Accepted", "endTime": "" }, { "status": "New", ...

Potential Javascript timing problem encountered during webpage initialization – involving the implementation of a dynamic bootstrap progress

I have limited knowledge of javascript, but I stumbled upon this amazing fiddle that I would like to incorporate into my project: http://jsfiddle.net/5w5ku/1/ The issue I am facing is that I want it to persist for a duration of ten minutes. Despite atte ...

Switching between components in vue.js: A beginner's guide

In my project, I created a Dashboard.vue page that consists of three child components: Display, sortBooksLowtoHigh, and sortBooksHightoLow. The Dashboard component also includes a select option with two choices: "Price: High to Low" and "Price: Low to High ...

When working with TypeScript, how do you determine the appropriate usage between "let" and "const"?

For TypeScript, under what circumstances would you choose to use "let" versus "const"? ...

"Attempting to access a variable defined in one pipe results in it being undefined in a

Currently, I am utilizing gulp to process pages for a website. Some of these pages are PHP files, however, after going through the template engine, they all end up with a .html file extension. My intention is to add a property to each file indicating if it ...

Tips for setting variable values in Angular 7

I'm encountering an issue with assigning values to variables in my code. Can anyone provide assistance in finding a solution? Here is the snippet of my code: app.component.ts: public power:any; public ice:any; public cake:any; changeValue(prop, ...

Avoid positioning issues when dropping a DIV onto the Canvas

Utilizing the JavaScript code below allows me to drag a DIV onto a canvas. I am currently loading a multi-page PDF, which consists of image files, in a pop-up window and positioning canvas layers over them. In these canvas layers, shapes are then loaded at ...

Close pop-up upon successful AJAX response in ASP.NET MVC

I have a modal in my view that allows me to create a new record in the database. The view for this functionality is contained within a partial view. Below is the code for the view: <script src="~/Scripts/jquery-3.1.1.js"></script> To han ...

"Enhance your database by incorporating HTML link clicks through AJAX integration with PHP and MySQL

After browsing through similar questions and attempting to implement it on my website, I'm facing an issue where the expected functionality is not working as intended. When users click on a link, there is no response in the console, and the database r ...

The error message from the mongoose plugin is indicating a problem: it seems that the Schema for the model "Appointment" has not been properly registered

I need help troubleshooting a mongoose error that is being thrown. throw new mongoose.Error.MissingSchemaError(name); ^ MissingSchemaError: Schema hasn't been registered for model "Appointment". Use mongoose.model(name, schema) I have double-c ...

Use JQuery to load a particular page by specifying its ID with the hashtag symbol

I am facing an issue where I need to create multiple private chatrooms per user. Although I have managed to make it work, I encountered a problem where if there are more than one private chats happening simultaneously, the content of these chats gets broad ...

What is the proper way to execute this API request and retrieve the latest news data using the Fetch method?

Note: Please note that the API Key used in this code is not a real one for privacy reasons. const url = 'http://newsapi.org/v2/everything?' + 'q=platformer&' + 'apiKey=3ce15c4d1fd3485cbcf17879bab498db'; ...

Discover the row and column of a selected table cell using vanilla JavaScript, no need for jQuery

In my JavaScript code, I am currently working on creating an onclick function that will display the row and column of a specifically clicked cell in a table. I have successfully implemented functionality to return the column number when the cell is click ...

I am having trouble grasping the concept of CSS and the first-child selector

I am having an issue with the first-child selector in CSS. I have two divs with the same class, each containing an image and a paragraph element. When I use the .div-class:first-child {margin:10} rule, the margin is only applied to the paragraph in the fir ...

Utilizing a PHP-scripted multidimensional array in an AJAX success function

I've encountered an issue while trying to access a multidimensional array passed to my AJAX success function. Here's how I'm sending the data: $response = array(); $response['message']['name'] = $name; $response['m ...