Generating values in a loop - mastering the art of creating data points

My goal is to create a variable within a loop:

box-shadow: 835px 1456px #FFF, 1272px 796px #FFF, 574px 1357px #FFF
and so on...

The concept is simple:

box-shadow: x1 y1 #fff, x2 y2 #fff, x3 y3 #fff, x4 y4 #fff ... xn yn #fff.

I attempted to achieve this by doing the following:

@maxPiont: 2000;
@minPoint: 0;

.init() {
    .make-point(`Math.random()`, `Math.random()`, ""); // set initial value - ignore for now
}
.init();
 .make-point(@newX, @newY, @old) {
    .redefine() {
      @X: floor(@newX * (@maxPiont - @minPoint + 1));
      @Y: floor(@newY * (@maxPiont - @minPoint + 1));
      @point: '@{X} @{Y} #fff';

      @allPoints: "@{point},  @{old}"; 
     }
}
.make-stars(@i) when (@i >0) { // unfortunately, in a loop we are in the same scope all the time and the trick won't work
    .redefine();
    .make-point(`Math.random()`, `Math.random()`, @allPoints); 
    .test
    {
        box-shadow: @allPoints;
    }
    .make-stars(@i - 1);
}


.make-stars(1); // <- scope 1
.make-stars(1);   // <- scope 2
.make-stars(1);   // <- scope 3
.make-stars(100);   // <- scope 4
.make-stars(1);   // <- scope 5
.make-stars(1);   // <- scope 6
.make-stars(1);   // <- scope 7

I understand why it's not working within the loop and why it works outside of it (due to scopes and less lazy loading). Is there another way to approach this?

I'm considering creating a variable step by step and adding it. Is this feasible or just a crazy idea?

Answer №1

If you want to aggregate shadow values from multiple properties into a comma separated list, you can utilize the merge function in LessCSS. There is no need to store all shadows into one variable.

Sample Less code:

@min: 1;   // Minimum random number
@max: 100; // Maximum

// Creating a single shadow with random position    
.random-shadow() {
  @x: `Math.floor(Math.random() * (@{max} - @{min}))`;
  @y: `Math.floor(Math.random() * (@{max} - @{min}))`;
  box-shadow+: unit(@x, px) unit(@y, px) #fff;
}

// Generating @count shadows for the element
.multiple-shadow(@count) when (@count > 0) {
  .random-shadow();
  .multiple-shadow(@count - 1);
}

a {
  .multiple-shadow(5);
}

Resulting CSS output:

a {
  box-shadow: 79px 81px #fff, 76px 98px #fff, 67px 97px #fff, 44px 25px #fff, 54px 11px #fff;
}

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

Material UI defaults remain unchanged despite any emotional influence

I'm currently experimenting with custom styling on a MaterialUI Typography component using the code snippet below: const StyledTitleTypography = styled(Typography)` color: 'black'; font-weight: 'bold'; `; <StyledTitleTypogr ...

Is it possible to adjust the color of a pseudo class to align with the color of a different class?

Creating a unique 'block' element on a webpage named 'test-div' has been quite the challenge. Adding an additional class to test-div changes the background color, such as: test-div test-div-bg-bright-gold test-div test-div-bg-dark-blu ...

What is the best way to apply CSS styles to a child div element in Next.js?

I'm currently working on enhancing the menu bar for a website project. Utilizing nextjs for my application I am aiming to customize the look of the anchor tag, however, encountering some issues with it not rendering correctly. Here is a snippet of ...

Move the location of the mouse click to a different spot

I recently received an unusual request for the app I'm developing. The requirement is to trigger a mouse click event 50 pixels to the right of the current cursor position when the user clicks. Is there a way to achieve this? ...

Creating a series of scalable svgs of uniform size in a row, enabling me to resize them based on width without disrupting the overall design

I need a row of equally sized SVGs with some text below them, like a navigation bar but larger. I almost have it working, but there are some issues with my current setup. To clarify, this is what I am aiming for: The SVGs should be responsive and scale a ...

Is there a way to place the icon on the right side of the @mui/Chip component?

Currently, I am working with MUI version 5.15.0. I have a component called Chip, and my goal is to display the icon after the label on the right side. I attempted to use the CSS rule - .MuiChip-icon{ order:1 }, but it resulted in too much spacing. Additio ...

Modify the width measurement from pixels to percentage using JavaScript

Looking for some help with a content slider on my website at this link: . I want to make it responsive so that it adjusts to 100% width. I managed to make it responsive by tweaking some code in the developer tools, but now I'm stuck. The bottom two p ...

jQuery User interface smooth scrolling feature

When my jQuery UI dialog is minimized, it moves to a container on the left side. How can I implement a custom jQuery UI scrollbar for that container? I attempted this approach, but only received the default bland scrollbar: #dialog_window_minimized_con ...

jquery mobile integrated seamlessly between image1 and image2

One issue I'm facing with jquery.mobile is that there seems to be a gap between photo1 and photo2 when displayed on my mobile device. Any suggestions on how to eliminate this space and have the images appear seamlessly next to each other? Thank you in ...

Creating a dynamic hover drop-down navigation bar in ASP.NET

After successfully creating a navigation bar with hover effects, I am now looking to implement a dropdown list when hovering over the items. Can anyone provide guidance on how to achieve this? Below is the current code snippet I am working with: <ul c ...

Is there a way to restore a minimized min.css file?

Recently, I attempted to utilize an online tool for unminifying my CSS code. However, when I replaced the minified code with the unminified version and accessed the URL, I discovered that the entire website had lost its layout. Unfortunately, relying sole ...

What is the best way to apply a CSS class to a div element without affecting its child elements using JavaScript?

Currently, I am using JavaScript to add and remove a CSS class through a click event. Within my div structure, I have multiple similar divs. While I can successfully add and remove the class from my main div, I am facing an issue where the class is also ap ...

Is the ID selector the quickest method in jQuery and CSS?

Which is the optimal choice in jQuery/javascript for speed? $('#myID .myClass') or $('.myClass') What is the preferred option to utilize in CSS? #myID .myClass{} or .myClass{} In hindsight, I realize my explanation was insuffici ...

Transitioning between pages causes a delay in loading the background

Today as I was working on my CSS, I encountered some issues. I utilized Bootstrap and Darkstrap for designing. In Darkstrap, the style for the body is body { color: #c6c6c6; background-color: #2f2f2f; } Meanwhile, in my own CSS: body { ...

Styling the active selection in nav class with bold attribute in Bootstrap/AngularJs

How can I apply the bold attribute to the currently selected nav bar item? <ul class="nav"> <li role="presentation" ng-repeate="item in items" ng-class="{'active':navLink == item.header}"> </li> &l ...

Responsive navigation menu featuring dynamic dropdown menus using HTML and CSS

Looking to design a responsive HTML and CSS navigation menu with multiple dropdown menus? You're in the right place. I've scoured countless YouTube videos, but haven't quite found the perfect template for a two-part navigation menu that tic ...

How can I alter the background color while performing a transformation in JS/CSS?

I'm currently working on achieving a flipboard effect for some divs, where one side is white and the other is black. Here's what I have so far: setInterval(function () { document.getElementById('test').classList.toggle('flipped& ...

Getting rid of all CSS properties currently set for the Mobile view

Utilizing third-party library components in my project. The library includes components that come with predefined styles for mobile devices. I am seeking to completely UPDATE these properties within my own code. When I say "update," I mean removing all th ...

Obtain the text that is shown for an input field

My website is currently utilizing Angular Material, which is causing the text format in my type='time' input field to change. I am looking for a way to verify this text, but none of the methods I have tried give me the actual displayed text. I a ...

Issues arise with animated content when viewed on browsers other than Chrome

After coding some jQuery, I noticed that the animation is working properly in Chrome but not in IE (version 11.0) or Firefox (32.0.3). Additionally, I found that if I omit 'opacity: 1;' from my animation class, the element will revert back to it ...