JavaScript causing attribute() variable to malfunction in CSS animation

I am attempting to implement a CSS animation using three files. Below is the HTML file:

<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" class = "tag" href="../css/style.css">
</head>
<body>
    <script type="text/javascript" src="main.js"></script>
    <!-- header => class => id => inline style-->
    <div class="main-content">
        <h2>HELLO WORLD</h2>
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nisl eros, 
pulvinar facilisis justo mollis, auctor consequat urna. Morbi a bibendum metus. 
Donec scelerisque sollicitudin enim eu venenatis. Duis tincidunt laoreet ex, 
in pretium orci vestibulum eget. Class aptent taciti sociosqu ad litora torquent
per conubia nostra, per inceptos himenaeos. Duis pharetra luctus lacus ut 
vestibulum. Maecenas ipsum lacus, lacinia quis posuere ut, pulvinar vitae dolor.
Integer eu nibh at nisi ullamcorper sagittis id vel leo. Integer feugiat 
faucibus libero, at maximus nisl suscipit posuere. Morbi nec enim nunc. 
Phasellus bibendum turpis ut ipsum egestas, sed sollicitudin elit convallis. 
Cras pharetra mi tristique sapien vestibulum lobortis. Nam eget bibendum metus, 
non dictum mauris. Nulla at tellus sagittis, viverra est a, bibendum metus
        </p>
    </div>
    <div id="bounceLeft"></div>
</body>
</html>

Next, I have the JavaScript file...

setInterval(()=>{
    document.getElementsByClassName("main-content")[0].setAttribute("width", window.innerWidth);
    document.getElementsByClassName("main-content")[0].setAttribute("height", window.innerHeight);}
, 1000);

Lastly, here is the CSS file.

@keyframes colChange{
    0% {background-color: red;}
    33% {background-color: rgb(0, 38, 255);}
    66% {background-color:  rgb(20, 192, 72);}
    100% {background-color: red}
}

@keyframes bounceLeft{
    0%{left: calc(attr(data-width)/2-230); top:0px;}
    50%{left: calc(attr(data-width)/2-200); top:attr(data-height);}
    100%{left: calc(attr(data-width)/2-200); top:0px;}
}

.main-content{
    height: 300px;
    width: 400px;
    margin: 0 auto;
    background: linear-gradient(360deg, rgba(255,0,0,0.5), rgba(255,0,0,0));
    outline-width: 7px;
    outline-color: yellow;
    outline-style: outset;
    
}

.main-content{
    height:auto;
}

#bounceLeft{
    position: relative;
    width: 25px;
    height: 25px;
    border: solid white;
    animation-name: bounceLeft;
    animation-duration: 4s;
    animation-iteration-count: infinite;
}

div :hover{
    outline-style: dashed;
} 

body{
    animation-iteration-count: infinite;
    animation-name: colChange;
    animation-duration: 10s;
}

p{
    text-align: center;
    color: rgb(214, 161, 16);
}
h2{
    text-align: center;
}

The issue I encountered is that the div is not moving at all. Upon inspection, I discovered that the attr() function in the CSS was not functioning correctly, but I am unsure of how to resolve it. I aim to create an animation where the div bounces vertically on the screen next to the placeholder text. Here is a screenshot of the assumed error pic of error

EDIT: The desired object for bouncing is the white box

Answer №1

setInterval(function(){  
   document.getElementsByClassName("main-content")[0].style.width = window.innerWidth + "px";
   document.getElementsByClassName("main-content")[0].style.height = window.innerHeight + "px";
}, 1000);
 var m = 200;
@keyframes colChange{
    0% {background-color: red;}
    33% {background-color: rgb(0, 38, 255);}
    66% {background-color:  rgb(20, 192, 72);}
    100% {background-color: red}
}

@keyframes bounceLeft{
    0%{left: calc(attr(data-width)/2-230); top:0px;}
    50%{left: calc(attr(data-width)/2-200); top:attr(data-height);}
    100%{left: calc(attr(data-width)/2-200); top:0px;}
}

.main-content{
    height: 300px;
    width: 400px;
    margin: 0 auto;
    background: linear-gradient(360deg, rgba(255,0,0,0.5), rgba(255,0,0,0));
    outline-width: 7px;
    outline-color: yellow;
    outline-style: outset;
    
}

.main-content{
    height:auto;
    transition: all 2s;
}

#bounceLeft{
    position: relative;
    width: 25px;
    height: 25px;
    border: solid white;
    animation-name: bounceLeft;
    animation-duration: 4s;
    animation-iteration-count: infinite;
}

div :hover{
    outline-style: dashed;
} 

body{
    animation-iteration-count: infinite;
    animation-name: colChange;
    animation-duration: 10s;
    
}

p{
    text-align: center;
    color: rgb(214, 161, 16);
}
h2{
    text-align: center;
}
<div class="main-content">
        <h2>HELLO WORLD</h2>
        <p>
            New content goes here.
        </p>
    </div>
    <div id="bounceLeft"></div>

Answer №2

It appears that the issue lies in the incorrect formatting of the CSS calc expressions. Any minus operator should have spaces before and after it. Additionally, division operations must be enclosed within a calc function.

Below are the corrected keyframes with proper spacing and calc functions included:

@keyframes bounceLeft{
    0%{left: calc(calc(attr(data-width) / 2) - 230); top:0px;}
    50%{left: calc(calc(attr(data-width) / 2) - 200); top:attr(data-height);}
    100%{left: calc(calc(attr(data-width) / 2) - 200); top:0px;}
}

Here is the snippet demonstrating the animation:

setInterval(()=>{
    document.getElementsByClassName("main-content")[0].setAttribute("width", window.innerWidth);
    document.getElementsByClassName("main-content")[0].setAttribute("height", window.innerHeight);}
, 1000);
@keyframes colChange{
    0% {background-color: red;}
    33% {background-color: rgb(0, 38, 255);}
    66% {background-color:  rgb(20, 192, 72);}
    100% {background-color: red}
}

@keyframes bounceLeft{
    0%{left: calc(calc(attr(data-width) / 2) - 230); top:0px;}
    50%{left: calc(calc(attr(data-width) / 2) - 200); top:attr(data-height);}
    100%{left: calc(calc(attr(data-width) / 2) - 200); top:0px;}
}

.main-content{
    height: 300px;
    width: 400px;
    margin: 0 auto;
    background: linear-gradient(360deg, rgba(255,0,0,0.5), rgba(255,0,0,0));
    outline-width: 7px;
    outline-color: yellow;
    outline-style: outset;
    
}

.main-content{
    height:auto;
}

#bounceLeft{
    position: relative;
    width: 25px;
    height: 25px;
    border: solid white;
    animation-name: bounceLeft;
    animation-duration: 4s;
    animation-iteration-count: infinite;
}

div:hover{
    outline-style: dashed;
} 

body{
    animation-iteration-count: infinite;
    animation-name: colChange;
    animation-duration: 10s;
}

p{
    text-align: center;
    color: rgb(214, 161, 16);
}
h2{
    text-align: center;
}
<div class="main-content">
    <h2>HELLO WORLD</h2>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nisl eros, 
        pulvinar facilisis justo mollis, auctor consequat urna. Morbi a bibendum metus. 
        Donec scelerisque sollicitudin enim eu venenatis. Duis tincidunt laoreet ex, 
        in pretium orci vestibulum eget. Class aptent taciti sociosqu ad litora torquent
        per conubia nostra, per inceptos himenaeos. Duis pharetra luctus lacus ut 
        vestibulum. Maecenas ipsum lacus, lacinia quis posuere ut, pulvinar vitae dolor.
        Integer eu nibh at nisi ullamcorper sagittis id vel leo. Integer feugiat 
        faucibus libero, at maximus nisl suscipit posuere. Morbi nec enim nunc. 
        Phasellus bibendum turpis ut ipsum egestas, sed sollicitudin elit convallis. 
        Cras pharetra mi tristique sapien vestibulum lobortis. Nam eget bibendum metus, 
        non dictum mauris. Nulla at tellus sagittis, viverra est a, bibendum metus
    </p>
</div>
<div id="bounceLeft"></div>

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

Align Text with Icon Using FontAwesome

Hey, I'm having a bit of trouble with something simple and it's driving me crazy. All I want to do is center the text vertically next to a FontAwesome icon. However, no matter what I try, I just can't seem to get it to work. I've looke ...

Utilizing multiple dropdown buttons in navigation menu items with Bootstrap 4

Trying to find a solution for having sub menus within the menu items. There are 2 dropdown buttons (Reports and Views) within a menu item that is itself a dropdown item. Clicking on the first button displays the submenu below, but clicking on the second ...

Display issue with positioning user name in navbar on Internet Explorer

The user name appears correctly in the navbar on Firefox and Chrome, but in IE it drops down due to a break tag issue. Is there a way to resolve this in IE so that it displays like in Firefox and Chrome? body { padding-top: 102px; background-colo ...

Hover to stop menu movement

Can someone help me achieve a similar menu hover effect like this one? I'm trying to create a menu with a hold/pause effect on hover, specifically in the section titled "A programme for every vision". The goal is to navigate through the sub menus smo ...

Creating an HTML element that can zoom, using dimensions specified in percentages but appearing as if they were specified in pixels

This question may seem simple, but I have been searching for an answer and haven't found one yet. Imagine we have an HTML element with dimensions specified in pixels: <div style="width:750px; height: 250px"></div> We can easily resize i ...

Ensure text within list items is positioned properly and not obscured by any decorative elements

I'm struggling to create top-bottom borders for list items with customized decorations. The text of the element should not be hidden under the decoration even if it's too long. Any ideas on how to achieve this? div { width: 20%; } ul ...

Deleting the stylesheet exclusively within the confines of the React application window

Here is an image that will help illustrate the issue: https://i.stack.imgur.com/VA7fw.png If you want to check out the code sandbox for this problem, you can visit: https://codesandbox.io/s/annoying-stylesheet-2gpejc?file=/public/index.html I am current ...

Transform the infoBox into a jQuery dialog modal window

In this section, I have integrated a map with markers and infoBoxes. My goal now is to replace the infoBoxes with jquery dialog() and modal window functionalities. How can I achieve this effectively? Below is the code snippet that includes the implementat ...

Issues with IE9's CSS hover functionality are causing problems

This particular css style functions well on most browsers, but seems to have compatibility issues with Explorer 9 specifically when it comes to the :hover effect. There are instances where it works perfectly fine and other times when it doesn't work a ...

Prevent horizontal scrolling on mobile devices

To prevent vertical scrolling on mobile devices, I attempted using the CSS code: body {overflow-x:hidden} This successfully disables vertical scrolling in regular browsers. However, if there is an element wider than the screen on mobile, it still allows ...

What is the best way to show information entered into a textbox on a different webpage?

Looking for advice on how to collect data in a text box and display it on another page? I have included HTML code for both the announcement page (where the data is entered) and the archive page (where the data should be shown). Could someone guide me on ...

Aligning an SVG within a container div

I am trying to display an SVG image inside a fixed-position div. Here is the HTML: <div class="main"> <svg class="svg" viewBox="0 0 180 100"> <rect height="100%" width="100%" fill="#003300"></rect> </svg> </div> ...

The footer is displaying unusual white space beneath it

Recently, I attempted to create a sticky footer using Flexboxes and the <Grid container> Check out the code on Codesandbox However, an issue arose - there was a strange whitespace below the footer. https://i.sstatic.net/2YdaJ.png After some exper ...

Tips for achieving a precise amount of boxes in each row

Is it possible to only have 5 boxes per line and then the next one goes to the next line using CSS? https://i.stack.imgur.com/L7vr4.png .box { border-color: #32ff00; border-width: 4px; border-style: dotted; width: 170px; height: 200px; fl ...

Ways to prioritize HTML5/CSS3 navigation menu above the content

I am attempting to position my navigation menu on top of the content. Despite using z-index and overflow = visible, the desired effect is not achieved. I want to avoid setting the position relative as it will push the content downwards. Simply put, I want ...

"Customizing the color of the arrow in Bootstrap tooltips

Trying to customize tooltips in twitter-bootstrap, I set the template option of the tooltip with inline styles to achieve red tooltips instead of the default black color: $(document).ready(function(){ options = { container: 'body&ap ...

Is there a way to incorporate a CSS file into this without explicitly mentioning the color?

I have successfully implemented a PHP solution for changing themes with a cookie that remembers the selected theme color when the user leaves the site. However, I now need to switch this functionality to JavaScript while still utilizing the CSS file. How c ...

html issue with the footer

Just starting out with HTML and encountering some difficulties with adding a footer. The main issue is that when I add the footer, the 'form' element gets stretched all the way down until it reaches the footer - you can see an example of this her ...

A step-by-step guide on toggling between light mode and dark mode using water.css

Implementing the water.css on my website, I have this JavaScript code: function setTheme(themeName) { localStorage.setItem('theme', themeName); document.documentElement.className = themeName; } // function to toggle between light and dark ...

Using Bootstrap 4, you can create nested rows that will automatically expand to fill their parent container, which in turn has been set

In my current setup, I have a div with the class "d-flex flex-column" that contains a navbar and a container. Within this container, there is another div with the class "d-flex flex-column" which then contains two rows. I am using flex-grow to make the con ...