Utilizing dynamic CSS classes within an ngFor loop

Struggling with my pure CSS star rating implementation, specifically with the hover effect. When hovering on one rating star, the behavior affects the first or all five stars of the previous question.

https://i.sstatic.net/cpjvw.png

In the provided image, attempting to rate the second question triggers the hovering effect on the first five stars of the initial question. Here is a snippet of the code:

<ng-container *ngFor="let x of evaluation_form; let i = index">
  <div class="row">
    ...
  </div>
  <div class="row" *ngFor="let y of x.segments; let j = index" [@hideShowAnimator]="i == hideShowAnimator">
    ...
      <fieldset class="rating">
        ...
      </fieldset>
    ...
  </div>
  <div class="row" (click)="hideShowAnimation(i)">
    ...
  </div>
</ng-container>

The CSS styling for the radio buttons used in the rating system:

.rating {
  float: left;
}

...

Seeking guidance on making the five-star rating dynamic for each question in Angular 2. Any assistance would be greatly appreciated. Thank you!

Answer №2

UPDATE

Apologies for the accidental inclusion of an angularJS answer. Here is the corrected Angular version.

In short; apply a class on hover and define styles for that class.

HTML

<fieldset class="rating">
    <input type="radio" (mouseover)="z.active=true" (mouseout)="z.active=false" id="star5" name="{{y.segment_name}}x{{z.q_id}}" value="5" (change)="changeRatingValue(i,j,o,5)" />
    <label [ngClass]="{'active': z.active}" for="star5"></label>
...
</fieldset>

CSS

.rating:not(:checked)>label.active:hover,
.rating:not(:checked)>label.active:hover~label {
    color: gold;
    text-shadow: 1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0, 0, 0, .5);
}

AngularJS

Try applying an active class on ng-mouseover and define the styles accordingly.

HTML

<fieldset class="rating">
    <input type="radio" ng-mouseover="z.active=true" ng-mouseleave="z.active=false" id="star5" name="{{y.segment_name}}x{{z.q_id}}" value="5" (change)="changeRatingValue(i,j,o,5)" />
    <label ng-class="{active: z.active}" for="star5"></label>
...
</fieldset>

CSS

.rating:not(:checked)>label.active:hover,
.rating:not(:checked)>label.active:hover~label {
    color: gold;
    text-shadow: 1px 1px goldenrod, 2px 2px #B57340, .1em .1em .2em rgba(0, 0, 0, .5);
}

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

storing the input values in a cache

When a user enters a query into the search form and submits it, they are taken to a results page displaying the search results. If the user then clicks on the browser's back button, I want them to be directed back to the search form with their previou ...

The confirmPasswordReset function in Angularfire auth is not defined in the User type

I am currently working on integrating password reset functionality using the angularfire2/auth class, following the steps outlined in this guide: https://medium.com/@c_innovative/implementing-password-reset-can-be-a-tricky-but-inevitable-task-737badfb7bab ...

What is the best way to align text in the center based on a specific portion of the content?

I'm working on an inline flex div where I want to create a unique effect using the words wrap and the div overflow. As the window is resized, some of the words in the sentence will disappear, altering the meaning. The width will determine the result: ...

Error: TweenLite has not been recognized

/justincavery/pen/mPJadb - this is a link to CodePen After copying the code from CodePen and running it, I encountered an error: "Uncaught ReferenceError: TweenLite is not defined". The image only draws once and there is no animation unless I press "F5 ...

Ways to contrast HTML without considering whitespace?

I'm currently testing an HTML builder through unit testing. My goal is to confirm that the output content matches the expected content, while allowing for some leniency when it comes to white space. More specifically, I am not concerned with whether ...

What is the best way to align two items where one is centered and the other is positioned to the left?

In an attempt to customize a chat call to action, I am looking to have an icon aligned to the left and some centered text. Here is an example of how I envision it: [ @ Text aligned in center ] Here is what I have tried so far: .icon{ justify-self: ...

Styling a Pie or Doughnut Chart with CSS

I am working on creating a doughnut chart with rounded segments using Recharts, and I want it to end up looking similar to this: Although I have come close to achieving the desired result, I am encountering some issues: Firstly, the first segment is over ...

Is there a way to determine if a container is overflowing at the top?

Currently, I am using Puppeteer to scrape Slack. My goal is to confirm whether I have scrolled to the very top of the channel feed. The issue arises because the channel feed does not actually scroll, making it impossible for me to utilize the method outli ...

Chrome not loading all the videos on a single page

Currently, I am working on a project that involves loading multiple videos on a single web page. The code snippet I am using looks like this: <div class="video_content left"> <span style="font-weight:bold">1</span> <video wi ...

Sliding Menu with jQuery

My goal is to create a dynamic menu that reveals sub-menu items by sliding them down and changing the background color upon clicking. I am currently using the HTML code displayed below, with the sub1 and sub2 elements initially hidden using display:none; ...

Tips for achieving an eye-catching text and image layout on a single page of your Wordpress blog

I've been exploring ways to achieve a design concept for my blog layout. I envision having the text and thumbnail displayed side by side, each taking up 50% of the width until the image reaches its end. Once the image ends, I want the text to span the ...

What is the best way to place two radio buttons side by side in a row?

How can I place two radio buttons in a single row with their labels, like the example shown in the picture? ...

Stop jQuery ajax from running any JavaScript code contained in a script or HTML response

As stated in the documentation: If html is specified, any embedded JavaScript within the retrieved data will be executed before returning the HTML as a string. Similarly, using script will execute the pulled back JavaScript before returning nothing. Is ...

Trigger a click event to alter the child div elements

I have a <div> that expands when clicked and shrinks back when clicked again. Inside this div are images that should only appear once the div expands. My issue is ensuring that all images except the first one are hidden until the div expands. https: ...

Dealing with jQuery's scrollTop viewport problem: Adding a personalized value to position().top

I'm currently working on a single-page WordPress site. Since it's all contained on one page, I've implemented jQuery scrollTop to animate the menu when clicked. However, I am facing an issue where the fixed menu is overlapping the content of ...

Why does this inner HTML table always adjust its width based on the content within it? Is there a way to make it match the width of its container instead

I'm not very familiar with HTML and CSS, and I've come across a problem. Here is the CSS structure in question: <!-- TECHNICAL REFERENCE: --> <div id="referenteTecnicoTab"> <table width="800px" class="standard-table-cls table-he ...

Having trouble with your CSS dropdown menu?

I've been struggling to implement a dropdown menu on my website following an online tutorial. Despite following the steps, I can't seem to get it working. It's frustrating because I want to move on to backend development. Hoping someone can ...

Guidelines for redirecting to ensure the presence of an HTML page within a directory URL

In my folder named /topicA/, I have various subtopics like /topicA/subtopic1.html, /topicA/subtopic2.html, and /topicA/subtopic3.html. I also wanted to create a /topicA.html page, but I'm having trouble getting the .htaccess file to function properly ...

(Enhancing Angular) Capture HttpResponse errors and seamlessly proceed with the Observable

There's a dropdown text box with type-ahead search functionality. Valid item names prompt the expected list of items in the drop-down menu, while invalid entries trigger a 400 error response from the API. This error is caught by the HttpErrorIntercept ...

How do I easily show/hide a submenu in AngularJS with a toggle menu?

After browsing some Stacks, I've come to the realization that I need to create separate directives in order to hide or toggle a menu from its sub-menu items. I want the menu to be hidden when any of the sub-menu items are clicked. However, I can&apo ...