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

I would like my division to split into two halves, each with a width of 50%, and be aligned next to each other using fxLayout

<div fxLayout="row" fxLayoutAlign="space-between" style="background-color: blue;"> <div fxLayout="column" style="width: 50%;">1stOne </div> <div fxLayout="column" styl ...

Arrange the labels and input fields within nested elements in a synchronized manner

My data is structured semantically as shown below: <div class="inputs"> <div class="top"> <h4>Top</h4> <label for="top-1">Label 1</label> <input id="top- ...

Facing difficulty observing Content-Disposition header in create-react-app project

I am using a create-react-app that is being served by express. Express is acting as a proxy for the app and all the application logic resides in CRA. My issue lies in calling an API to download a file. The API sends back a "Content-Disposition" header wit ...

Creating a line between two points in raphael

Hello there, I'm looking to create a line between two points in Raphael. Could you please provide me with some examples or guidance on how to achieve this? Thanks a lot!) ...

I am having trouble retrieving a JsonResult from an asp.net mvc controller using $resource in angular

I am new to Angularjs and trying to integrate it with asp.net mvc. I am facing an issue where I am unable to access an asp.net mvc controller to return a JsonResult using $resource in angular. Strangely, when I use $.getJson in JavaScript directly, it work ...

What is the best way to retrieve the current complete URL in a Next.js/Typescript component?

I'm working on a component and I need to retrieve the current full URL. Here's a simplified version of what I have: /** * Share dropdown component */ export const ShareDropdown: React.FC<{ className: string }> = ({ className, }) => { ...

The default value is not displayed in the Angular dropdown menu

When using regular html select menus, if you create an option element with selected and disabled attributes and provide text for that option, the text will be displayed by default in the select menu. Below is a basic example of HTML code: <select name= ...

Discover the magic of gradients in tooltips with Bootstrap 5!

I recently encountered some challenges when attempting to apply a gradient color to bootstrap 5 tooltips CSS: .tooltip .tooltip-arrow::before { position: absolute; content: ""; border-color: transparent; border-style: solid; } .bs-toolt ...

What could be causing my grid-area properties to not take effect?

I'm attempting to create a basic structure with a header in the top row, menu and content in the middle row, and footer at the bottom. Here is what I have so far: body { color: white; } .container { background: cyan; display: grid; ...

``Trouble with React Dropdown menu option selection"

I am encountering challenges with implementing a dropdown menu list for my react app. The issue at hand is that I have an API where one of the keys (key3) has values separated by commas that I wish to display in my dropdown list. The structure of the API ...

Is my jQuery code generating a large number of DOM objects?

I am currently developing a hex dumper in JavaScript to analyze the byte data of files provided by users. To properly display a preview of the file's data, I am utilizing methods to escape HTML characters as outlined in the highest-rated answer on thi ...

Is there a glitch in the three.js loadOBJMTL loader?

Encountering an issue with the OBJMTL loader in three.js. I'm working with obj/mtl/jpeg files and getting load errors that look like this: "THREE.OBJMTLLoader: Unhandled line 4033/5601/6659" OBJMTLLoader.js:347 Seems like there is a problem with a c ...

Is it possible to adjust the center of rotation in THREE.js TrackballControls so that it is not located at the center of the canvas

I'm currently using TrackballControls within THREE.js. The issue I am facing is that the center of rotation always remains in the center of the canvas. Is there a way to adjust this and move the center of rotation upwards? Here are my failed attempts: ...

Is it possible to transfer elements from one array to another when clicked, but without copying the contents to the new array objects?

Welcome, For my latest project, I am excited to create a "Learning Cards" App from scratch. The concept is pretty straightforward: it consists of cards with questions. Upon clicking a button, you can reveal the correct answer. Additionally, there's a ...

"Object.entries seems to be returning only the initial object in the list

This is my object var obj = { "first_obj": { "a":1, "status": 1 }, "second_obj": { "a":2, "status": 3 } } I'm struggling to loop through this object using foreach and Object.entries, as the latter only returns the first object. How ...

The dynamic addition of the active class is malfunctioning

I have dynamically added a class using AngularJS, but it seems to not be functioning correctly. As I am new to AngularJS, I would appreciate any suggestions to resolve this issue. Below is my HTML code: <div class="col-md-15 col-sm-3" ng-repeat="data i ...

Having trouble with Node.js POST request; no specific error message displayed

Currently facing a challenge in communicating with an API using nodejs. I've exhausted all possible solutions, including utilizing different request modules and altering the format among other attempts. Here is the code snippet... var request = requ ...

What is the best method for combining this function?

Currently, I am working on a code snippet that generates a sitemap.xml file when the /sitemap.xml endpoint is accessed. database = firebase.database(); var ref = database.ref('urls'); ref.on('value', gotData, errData); fu ...

What is the name of this JavaScript function declaration syntax? (var) => {}

While perusing the most recent NodeJS documentation, I stumbled upon a novel approach to defining a function: fs.unlink('/tmp/hello', (err) => { if (err) throw err; console.log('successfully deleted /tmp/hello'); }); Source: ht ...

What is the best way to ensure that a mapped type preserves its data types when accessing a variable?

I am currently working on preserving the types of an object that has string keys and values that can fall into two possible types. Consider this simple example: type Option1 = number type Option2 = string interface Options { readonly [key: string]: Op ...