Discover the secret to applying a gradient shade to the "border" element when it reaches full capacity with Vue's innovative Custom CSS package

Utilizing the package https://www.npmjs.com/package/vue-css-donut-chart#usage-with-all-the-available-props to create a "border" effect around images based on progress has presented a challenge. Specifically, achieving a gradient color for the border when it reaches 100% is not supported by the props provided. In an attempt to address this issue, the following workaround was implemented:

if(this.sections[0].value === 50) {
    this.sections[0].color = "transparent";
    this.sections[1].color = "transparent";
    let grad = document.querySelector(".cdc");
    grad.classList.add("border-gradient");
    console.log(grad);
}

The component is used in multiple locations within the application, but the gradient color only takes effect at one specific place:

<Home>
<sidebar/> <-----

<router-view />
</Home>

Main.js

import Donut from "vue-css-donut-chart";
import "vue-css-donut-chart/dist/vcdonut.css";
Vue.use(Donut);

Donut component

<template>
<div>
     <vc-donut
          :sections="sections"
          :size="size"
          :start-angle="angle"
          :thickness="thickness"
          :foreground="'#D4DDFC85'"
        >
        <b-img v-if="profilePic" class="object-fit-contain rounded-circle p-1 m-0" :width="width" :src="profilePic"/>
        </vc-donut>
        </div>
</template>

<script>
import backend from "@/services/backend.js";

export default {
    props: ["profilePic", "size", "width", "thickness"],
 data() {
    return {
        me: null,
        angle: null,
      sections: [
        { value: 0, color: "#4ECB62" },
        { value: 0, color: "#223C9B" },
      ],
    };
  },
  mounted() {
      this.fetch_me();
  },



  methods: {
      async fetch_me() {
          this.me = (await backend.me()).data;
         
          
       
         if(this.me.roles.indexOf('investor') != -1 && this.me.roles.indexOf('homeowner') != -1) {
            // Set values for homeowner depending on amount of listed properties
           /*  if(this.me.homes.length == 100) {
                 this.sections[1].value = 50
             } else if (this.me.homes.length >= 25) {
                 this.sections[1].value = 40
             } else if (this.me.homes.length >= 5) {
                 this.sections[1].value = 35
             } else {
                 this.sections[1].value = 25
             }*/

             this.sections[1].value = 50;
               this.sections[0].value = 50;
               if(this.sections[0].value === 50) {
                   this.sections[0].color = "transparent";
                   this.sections[1].color = "transparent";
                   let grad = document.querySelector(".cdc");
                   grad.classList.add("border-gradient");
                   console.log(grad);
               }
            // Set the correct angle depending on the value (Investor & Homeowner)
                 if(this.sections[0].value == 40 || this.sections[1].value == 40) {
                    return this.angle = 35;
                 } else if (this.sections[0].value == 50 || this.sections[1].value == 50) {
                    return this.angle = 0;
                 } else if (this.sections[0].value == 35 || this.sections[1].value == 35) {
                    return this.angle = 90;
                 }  else if (this.sections[0].value == 25 || this.sections[1].value == 25) {
                    return this.angle = 25;
                 }
            
          } else if(this.me.roles.indexOf('homeowner') != -1) {
            if(this.me.homes.length == 100) {
                 this.sections[1].value = 100;
             } else if (this.me.homes.length >= 25) {
                 this.sections[1].value = 85;
             } else if (this.me.homes.length >= 5) {
                 this.sections[1].value = 65;
             } else {
                 this.sections[1].value = 50;
             }
        
    
            // Set the correct angle depending on the value (Homeowner)
                if(this.sections[1].value == 50) {
                    return this.angle = 90;
                } else if(this.sections[1].value == 65) {
                    return this.angle = 62;
                } else if(this.sections[1].value == 85) {
                    return this.angle = 27;
                } else {
                    return 0;
                }
          } else if(this.me.roles.indexOf('investor') != -1) {
              this.sections[0].value = 50;
              if(this.sections[0].value == 50) {
                this.sections[0].value = 50;
              }
              // Set the correct angle depending on the value (Investor)
              if(this.sections[0].value == 50) {
                    return this.angle = 90;
                } 
          }
      },

  }
}
</script>

The console.log(grad) shows that the class is added to the element, but the CSS isn't applied.

Example of use of the Donut(ProfileImageRings) component

<profile-image-rings v-if="user.picture" :width="160" :size="170" :thickness="7" :profilePic="user.picture"/>

custom.scss

.border-gradient {
    background: linear-gradient(90deg, #223C9B 0%, #4ECB62 100%) !important;
}

Answer №1

The issue arose due to my use of querySelector, which only retrieves the first element that matches the selector.

The solution involves switching from querySelector to getElementsByClassName, then iterating through elements with the matching class and applying the desired class to each one.

this.sections[1].value = 50 
   this.sections[0].value = 50
   if(this.sections[0].value = 50) {
   this.sections[0].color = "transparent"
   this.sections[1].color = "transparent"
   let grad = document.getElementsByClassName("cdc")
      for(let i = 0; i < grad.length; i++) {
          grad[i].classList.add("border-gradient")
   }     
}

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

How to style large numbers with AngularJS

I'm facing a challenge with large numbers in a table and I'm seeking a solution to format them, ideally as $13b, $10m... Has anyone encountered this issue before and discovered a customized filter or solution for it? Appreciate any help! ...

How come ng-class doesn't apply to a specific class name?

I recently wrote the following code: <style> .dotted { border:dotted; } </style> .... <p ng-style="mystyle" ng-class="dotted">{{ answer }}</p> My intention was to have the paragraph element enclosed within a ...

What would be the ideal technology stack (modules, frameworks) to use for delving into node.js and creating a successful first project that allows for learning and growth?

A year ago, I took my first small steps into the world of Node.js. Back then, I was overwhelmed by the sheer number of modules and frameworks available. Now, I am determined to dive deeper into the Node environment and kickstart a web-based project that wi ...

Elements that are positioned in a single line in a text

I am experimenting with a layout where each item is displayed in a single line, with the width of the items determined by their content. The desired output looks like this: Intended result: (This feature is only supported in modern browsers) .item { ...

What is the best way to ensure that a header with SVG graphics is SEO-friendly and accessible to screen readers?

My website currently features an <h1> tag with an SVG logo, but unfortunately it is not accessible to webcrawlers and screen readers. What steps can I take to ensure that it can be properly accessed by them? ...

Using ReactJS to trigger a timer function on a button click

Kindly Note: This is a unique query and not related to ReactJS - Need to click twice to set State and run function. The suggested solution there does not resolve my issue. This represents my original state: constructor(props) { super(props) this. ...

problem with creating a django template script

Hello, I am currently working on a code that is responsible for executing various functions within the template. I have utilized scripts to verify these functions using if-else statements and for loops. However, I am encountering some errors during this pr ...

How can I add text to a textbox within a duplicated div using Javascript or Jquery?

I'm looking to add text to a cloned div's text box using jQuery. I have a div with a button and text box, and by cloning it, I want to dynamically insert text into the new div. $(document).ready(function () { $("#click").click(function () { ...

Menu stack with content displayed to the right on larger screens

Given the content div's position (up middle), what method can I use to stack the divs in this manner? Is it possible to accomplish with only CSS or is jQuery necessary to move the div content out on larger screens? ...

I am selecting specific items from a list to display only 4 on my webpage

I am trying to display items from a list but I only want to show 4 out of the 5 available items. Additionally, whenever a new item is added, I want it to appear first on the list, with the older items following behind while excluding the fifth item. Despi ...

`Node.js: Implementing intricate routes with Express`

Just starting out with Node.js and Express here. I have a question about setting up routes in Express.js. In my project, I need to match the following URL: example.com/?campaign=123&click=123. I've tried using the following condition in the app ...

The placement of footers remains consistent

Struggling with the placement of my footer on a website I'm designing. The footer should be at the bottom of the page with a set percentage margin, but it's not working as expected. Here is a link to My website Reviewing the footer CSS: .foote ...

React page is not loading properly after refreshing, displaying unprocessed data instead

Hello everyone! I am currently working on developing an app using Node, React, and Mongoose without utilizing the CRA command, and I have also incorporated custom webpack setup. Initially, I was able to build everything within a single React page (App.jsx ...

The state variables of React components do not always retain their most recent value

Currently, I am working on implementing the functionality of an event based library called powerbi-client-react. The first step involves retrieving the component using getEmbeddedComponent and storing it in the variable report. Then, I need to utilize the ...

Having trouble with preventDefault() not working during a keyup event?

I am struggling to make preventDefault() function properly. Here are a few variations of the code that I have attempted: First: $(document).keyup(function (evt) { var charCode = (evt.which) ? evt.which : event.keyCode; if (charCode == 192) { ...

Iterating through a list using curly braces in a vue for loop

I encountered a scenario where I need to iterate through items without generating any HTML. My anticipation is for the code to resemble something like this: <table id="detailTable"> <tr> <th class='editRow' ...

Utilizing Axios to pass multiple values through a parameter as a comma-separated list

Desired query string format: http://fqdn/page?categoryID=1&categoryID=2 Axios get request function: requestCategories () { return axios.get(globalConfig.CATS_URL, { params: { ...(this.category ? { categoryId: this.category } : {}) } ...

I need the PHP code to ensure that only checkboxes can

I keep encountering what seems like a simple issue in my html form with checkbox groups. I am trying to ensure that at least one checkbox is selected from each group, and if not, display an error message using PHP code. However, despite multiple attempts, ...

The React OnClick and onTouchStart event handlers are functioning properly on a desktop browser's mobile simulator, but they are not responsive when

I added a basic button tag to my next.js main page.js file that triggers an alert when clicked. Surprisingly, the onClick function is functional on desktop browsers but fails to work on any mobile browser. "use client"; export default function P ...

Updating URL links with PHP and DOM manipulation

I am attempting to transform all the links within an HTML variable into random ones using PHP. However, I am encountering an issue that is preventing the links from being properly changed. Below is the code snippet I am working with: <?php $jobTempla ...