Lack of animation on the button

Having trouble with this issue for 48 hours straight. Every time I attempt to click a button in the top bar, there is no animation. The intended behavior is for the width to increase and the left border color to change to green, but that's not what's happening. I am currently working with Svelte and am still inexperienced, hence the code quality is comparable to a PowerPoint presentation.

<script lang="ts">
  export let name;

  function onTopBarBtnClick(tab) {
        let tabButton = document.getElementsByClassName('topbarbtn ' + tab)[0]
        tabButton.classList.add('selected')
  }
</script>

<main>
  <nav class='topbar'>
    <label class="websitenameinfo">{name}</label>

    <button on:click={() => onTopBarBtnClick('1')} class="topbarbtn 1">Servers</button>
    <button on:click={() => onTopBarBtnClick('2')} class="topbarbtn 2">Events</button>
    <button on:click={() => onTopBarBtnClick('3')} class="topbarbtn 3">Store</button>
    <button on:click={() => onTopBarBtnClick('4')} class="topbarbtn 4">Info</button>
  </nav>
</main>

<style>
    .topbar {
        position: absolute;
        width: 100vw;
        height: 8vh;
        top: 0vh;
        left: 0vw;
        background-color: #1c1c1c;
        box-shadow: 1vh 1vh 2.8vh rgba(28, 28, 28, 0.8);
    }
    .websitenameinfo {
        position: absolute;
        top: 1vh;
        left: 1vw;
        text-transform: uppercase;
        font-family: Tahoma;
        font-size: 4.7vh;
        background: -webkit-linear-gradient(#2071cd, #0ac7ba);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      text-shadow: 0px 0px 3vh #579dffb3;
        font-weight: 800;
    }
    .topbarbtn {
        width: 10.1vw;
        height: 8vh;
        float: right;
        border-radius: 0px;
        background: -webkit-linear-gradient(#20cd31, #0ac7ba);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      text-shadow: 0px 0px 3vh #15ffbd5e,
                                 0px 0px 1.5vh #1573ff66;
        font-weight: 600;
        font-family: Tahoma;
        font-size: 3.8vh;
        border-image: linear-gradient();
        border-width: 0px;
        border-left-width: 0.1vw;
        border-color: rgb(45, 45, 45);
        color: rgb(52, 52, 52);
        vertical-align: top;
    transition: all 0.3s cubic-bezier(0.075, 0.82, 0.3, 1);
    }
    .selected {
            border-color: #0af05e;
            width: 15vw;
            border-left-width: 0.27vw;
            text-shadow: 0px 0px 3vh #6fffd6da, 0px 0px 1.6vh #84ff106d;
    }
</style>

After exhausting all my options available online, I still couldn't resolve the issue. My anticipation was for the buttons to widen and the left border color to switch to green upon clicking. However, it seems that nothing happens whatsoever when I click on them. It may be a problem with how I'm adding 'selected' to the class list, although I can't say for certain.

Answer №1

When working with Svelte, it's best to steer clear of directly manipulating the DOM through methods like

document.getElementsByClassName()
. Instead, utilize Svelte's reactive capabilities to handle state management and apply dynamic classes based on that state. Here's a sample implementation:

<script lang="ts">
  import { onMount } from 'svelte';

  let selectedTab = null;

  function onTopBarBtnClick(tab) {
    selectedTab = tab;
  }

  // Define a function to remove the 'selected' class when the component mounts
  onMount(() => {
    return () => {
      selectedTab = null;
    };
  });
</script>

<main>
  <nav class='topbar'>
    <label class="websitenameinfo">{name}</label>

    <button on:click={() => onTopBarBtnClick('1')} class:selected={'1' === selectedTab} class="topbarbtn">Servers</button>
    <button on:click={() => onTopBarBtnClick('2')} class:selected={'2' === selectedTab} class="topbarbtn">Events</button>
    <button on:click={() => onTopBarBtnClick('3')} class:selected={'3' === selectedTab} class="topbarbtn">Store</button>
    <button on:click={() => onTopBarBtnClick('4')} class:selected={'4' === selectedTab} class="topbarbtn">Info</button>
  </nav>
</main>

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

What is the best way to sort a table by column index using HTML?

I find myself in a situation where I am not well-versed in HTML, Javascript, and CSS. Here is the scenario: <div class="table"> <table class="display-table"> <thead> <tr> ...

Set my click event handler back to its default setting

I'm struggling with resetting a click function after it completes. How can I make sure it's ready to run again? $('body').on('click', '#ConfirmBet', function () { function randomImg() { var imgs = $(&apo ...

Struggling to properly center the footer on my Django template

I'm currently facing an issue when attempting to align my footer in the center of my Django template. The specific class for the footer is Pagination. Base HTML: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict ...

Issue with jquery's .load() and getScript functions

Earlier today, I encountered an issue with a .load() function being called in a script. I managed to solve the problem using .getScript(), but now I'm facing a major issue - the function is being executed multiple times. You can find the full code a ...

Updating HTML label values using jQuery in a loop based on their index position

I am having an issue with my ajax call where I want to loop through each label in a class and set their value to the response returned from the server. However, the current code sets all labels to the same value instead of setting individual values based o ...

Issues with jQuery Ajax functionality in Rails application not achieving successful completion

When a card is moved onto another stack, an Ajax call is triggered twice. This only happens when there are multiple stacks. However, if the cards are rearranged within the same stack, the call is triggered only once. The Ajax call sends an array of IDs fo ...

"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 ...

How can you assign a default value in a React select dropdown and dynamically add the remaining values?

I'm currently developing a React application that includes a form with a select dropdown feature. My goal is to have the 'uom' field value from the selected product record automatically set as the default option in the dropdown when the user ...

Update the innerHTML content dynamically every 5 seconds with Vue.js

I'm working on a new website and I'd like to spice things up by changing the header text with a random word every 5 seconds. Here's the starting point: const header = document.querySelector('.header') const needs = ['jacket& ...

A step-by-step guide on how to insert an image URL into the src attribute using the

The source of my image is -> src/assets/images/doctor1.jpg I would like to use this image here -> src/components/docNotes/docNotes.js In the docNotes.js file, I attempted -> <Avatar className={classes.avtar} alt="Remy Sharp" src ...

Providing dynamic string outputs depending on the current date using AngularJS

I set up an advent calendar that reveals a new question every day, but currently the questions aren't showing up. Here's the code I have: The controller located in app.js: .controller('textCtrl', function($http) { this.data = {}; ...

The link function of an AngularJs directive is unable to access the attribute value

I am facing an issue with a directive in AngularJS. return { restrict: _restrict, link: function (scope, element, attrs) { $timeout(LinkPre, 0); //Calling a scoped method }, templateUrl: Con ...

Optimal Placement for FB.Event.subscribe

Here is the code I have implemented on my website for the Facebook Like and Share buttons. The functionality works seamlessly. When I click the Like button, a notification is promptly displayed on my Facebook profile page. Additionally, Facebook automatic ...

Error: Chrome is preventing an AJAX request

Hello everyone, I have been facing an interesting issue with my Ajax request. It seems to work perfectly fine in Internet Explorer, which is quite surprising. However, when I attempt to run the same code in Chrome, I encounter the following error: XMLHt ...

Why is the lifecycle callback not being triggered?

I am currently learning how to develop with Vue.js. I have been trying to use the lifecycle callbacks in my code. In my App.vue file, I have implemented the onMounted callback. However, when I run the code, I do not see the message appearing in the consol ...

The functionality of "subscribe()" is outdated if utilized with "of(false)"

My editor is flagging the usage of of as deprecated. How can I resolve this issue and get it working with of? public save(): Observable<ISaveResult> | Observable<boolean> { if (this.item) { return this.databaseService.save(this.user ...

Tips for keeping several buttons in the spotlight: HTML/CSS/JS

Looking for a solution to keep multiple buttons toggled on? I'm not very familiar with JavaScript, but I know that adding a class on click won't work if there's already a class set. Maybe giving the element an ID could be a possible solution ...

Dynamically add a plugin to jQuery during execution

After installing jQuery and a jQuery-Plugin via npm, I am facing the challenge of using it within an ES6 module. The issue arises from the fact that the plugin documentation only provides instructions for a global installation through the script tag, which ...

What is the best way to save the output of an asynchronous function into a class attribute?

Currently, I am attempting to retrieve HTML content from a webpage by utilizing a class equipped with a single asynchronous method. This process involves Typescript 3.4.3 and request-promise 4.2.4. import * as rp from 'request-promise'; class H ...

Utilize the power of REACT JS to transform a specific segment within a paragraph into a hyperlink. Take advantage of the click event on that hyperlink to execute an API request prior to

In React JSX, I'm encountering an issue trying to dynamically convert a section of text into an anchor tag. Additionally, upon clicking the anchor tag, I need to make an API call before redirecting it to the requested page. Despite my attempts, I have ...