What is causing the issue with the variable in the CSS code?

I established a var variable called --item-width in JavaScript. I assumed it would function correctly in the CSS, but unfortunately, it's not working as intended. What steps should I take next? Being a novice, I probably made a mistake somewhere. Your assistance is greatly appreciated.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>marquee_text</title>
    <link href="CSS/style.css" type="text/css" rel="stylesheet">
    <script src="js/script.js" defer></script>
</head>
<body>
    <div id="wrap">
        <div class="marquee-text">
            <div class="marquee-text__wrapper" data-controller="marquee">
                <a href="#" class="item-link" target="_blank"></a>
                <div class="marquee-text__item">
                    <strong>Marquee Text</strong>
                    <svg class="marquee-text__separator" height="19" width="19" viewBox="0 0 100 95.11">
                        <polygon points="50 0 65.45 31.31 100 36.33 75 60.7 80.9 95.11 50 78.86 19.1 95.11 25 60.7 0 36.33 34.55 31.31 50 0"/>
                    </svg>
                    <span>Marquee Text</span>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
@charset "UTF-8";

:root {
  --item-width: 0;
}

.marquee-text {
    position: relative;
    width: 100%;
    display: flex;
    align-items: center;
    overflow: hidden;
    white-space: nowrap;
    height: 50px;
    background-color: #222;
    font-size: 17px;
    color: #fff;
}

.marquee-text__wrapper {
    display: flex;
    align-items: center;
    height: 100%;
}

.marquee-text__item {
    padding: 0 10px;
}

.marquee-text__separator {
    position: relative;
    top: -1px;
    margin: 0 10px;
    font-size: 1.3em;
    vertical-align: middle;
    fill: currentColor;
}

.marquee-text a:hover {
    opacity: 0;
}
@keyframes marquee-text-ani {
  0% {
    transform: translate3d(0, 0, 0);
  }
  100% {
    /* This snippet doesn't seem to be functioning properly. */
    transform: translate3d(-(var(--item-width))'px', 0, 0);
  }
}
setTimeout(()=>{
    this.move()
}, 200)

function move() {
    const $marqueeTop = document.querySelector(".marquee-text__wrapper");
    const $marqueeTopItem = document.querySelector(".marquee-text__wrapper>div");
    const $root = document.querySelector(":root");
    const itemWidth= $marqueeTopItem.clientWidth;
    
    for (i = 0; i < 10; i++) {
        let marqueeTopItem = $marqueeTopItem.cloneNode(!0);
        $marqueeTop.appendChild(marqueeTopItem);
    }
    
    // The variable mentioned here seems to be having issues interacting with CSS.
    $root.style.setProperty(`--item-width`, itemWidth);
    $marqueeTop.style.animation = `marquee-text-ani 4s linear 1s forwards infinite`;
}

Answer №1

After following Terry's advice and making adjustments to the code, everything is now working perfectly. Much appreciated.

transform: translate3d(calc((-1px) * var(--item-width)), 0, 0);

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

Error in Django: NoReverseMatch for Dynamic Links

I am in the process of creating pages dynamically based on the movie title of the clicked link. However, I am encountering an error that says "u'app_name' is not a registered namespace" when trying to set the link. Within my index.html, everythi ...

Issues with the functionality of Angular Factory

Issues are arising with the controller not retrieving data correctly from the factory to display in the view. When all the data was stored in the controller, it worked fine. However, upon transferring it to the factory, the data no longer showed on the vie ...

Customize cursor using CSS

I have a div with overflow: hidden that I am making scrollable by allowing the user to click and drag the background. There are links and buttons within this space. Here is the CSS I am using for this: #div-grabscroll { cursor: url(../img/op ...

merge a pair of scopes within Angular

I have a dilemma with merging two different scopes. Can someone suggest the most efficient method to achieve this? Here are details of the scopes in question; the second one originates from a service. The initial scope retrieves information from the datab ...

The external hyperlink is not functional in Firefox and Chrome browsers, but it operates properly in Internet Explorer 9

For security reasons, it seems that embedding an external link for live scoring from itftennis.com on our website is blocked. If you try to click on the jsfiddle LIVE SCORING link, it will open a page with an error. However, if you click on the address ba ...

The Bootstrap Navbar dropdown list appears cluttered on desktops but remains tidy on mobile devices

I am encountering an issue because I haven't studied much about the design aspect. I am currently using a template from w3layouts.com under the Education section, specifically the Tutoring Template. This template is based on Bootstrap. The demo page ...

Remove div blocks in a Django template when scrolling

How can I dynamically remove div blocks in django templates (queryset) as soon as they are out of the browser's field of view while scrolling? For example, delete blocks from the top when scrolling down and delete blocks from the bottom when scrolling ...

Using TypeScript to validate the API response against specific types

I'm intrigued by the scenario where you expect a specific data type as a response from fetch / Axios / etc, but receive a different type instead. Is there a way to identify this discrepancy? interface HttpResponse<T> extends Response { parsed ...

Restrict the amount of cards allowed in each card deck using Bootstrap 4

Is there a way to display up to 2 cards in a card deck and if there are more cards, have them displayed in a new deck below the previous one? How do I set the maximum number of cards for each deck? Where can this be specified? Here is a snippet of the HT ...

Inaccuracy of Canvas Drawing in Adobe Flash CC HTML5 Publishing

Let's start with the images: Comparison Of Various Publishing Techniques From Flash CC There seems to be a significant discrepancy between what is published through Flash CC HTML5 with CreateJS and what is actually created in the program, even thoug ...

I am working on an HTML form that is designed vertically, but I am unsure of how to arrange two text fields side by side on the same line

I'm struggling with formatting my HTML form to have two text fields on the same line instead of stacked vertically. In the example below, I want the Size, Width, and Height fields to be aligned horizontally rather than one below the other. <form c ...

Webview displaying a blank screen following the most recent Chrome update

I am encountering a problem that has been asked previously, but I am struggling to find a solution. The issue arises when I try to play an embedded video URL on the Webview and filter out other undesirable URLs using shouldOverrideUrlLoading. Whenever I cl ...

Refactor these codes by utilizing a single loop in React

Can you help me with reducing the amount of code in this function? The code provided below is functional and does not have any issues. flag : boolean, likeArr: ["A", "B", "C"], dislikeArr: ["D", "E", "F"] likeOrDislike( flag, likeArr, dislikeArr ) { ...

No content in Axios response

axios.post( 'http://localhost:3001/users', { username:user.username } ).then((res)=> console.log(res.data)) Response From FrontEnd : data: &qu ...

Loop through a JSON file in Vue

Struggling to successfully iterate over an external JSON file in Vue. The file is imported like this: import json from '../../public/platform.json' export default { data: () => ({ currentPage: 0, brand: & ...

Is there a method to dynamically incorporate a new editable textfield row in a react table?

Is there a way to dynamically add an editable row of text fields to a table in React? Currently, when I click on the "Add" button, a new row is added to the table but it's not editable by default. The logic for adding a new row is implemented inside t ...

Using Jquery to add a list after parsing JSON data stored in localStorage

I've been stuck on this issue for quite some time now. The problem I'm facing involves checking the localStorage to see if there's a cached JSON string available. If there is, I load it and convert it back into a JSON object. If not, I make ...

Utilizing yield (generators) in conjunction with selenium webdriver promises: A comprehensive guide

Trying to harness the power of generators in node 0.11.x to streamline my Selenium tests has proven to be a bit challenging. Despite using the official selenium-webdriver module (ver 2.37.0) and co (ver 2.1.0) for generator creation, I find myself struggli ...

Access Sharepoint from an external site

Looking for assistance with a SharePoint list containing columns for Name, Position, Office, and Salary. Upon logging in with specific credentials to the SharePoint website, I need to retrieve all items from the list and showcase them on my own website. ...

Want to display a button depending on the page you are navigating from in Ionic?

My home page displays a list of items, and whenever I add a new item, I want it to return to the home page. However, if I navigate back to the root from the addItemPage, I also want to display a button for saving the list and making an HTTP call. I'm ...