简体   繁体   English

随机将类添加到两个div

[英]Randomly add class to two divs

I have a two column red/black grid 50/50% and height 100% and had a script to randomize the appearance of both when loading the page, so left or right. 我有一个两列红色/黑色网格50/50%和高度100%,并有一个脚本,以便在加载页面时随机化两者的外观,所以左或右。 I changed the code behind the two column grid a bit going from relative position and float left of right to a fixed and absolute position. 我改变了两列网格后面的代码,从相对位置开始,向右浮动到固定绝对位置。 This has to do because of the scrolling behavior when in mobile this way it works nicer. 这必须做,因为在移动时这种滚动行为这种方式更好。

The code below worked fine when using floating it adds a class left or right that makes the red side choose a random side and the black automatically follows, because it is relative to each other. 下面的代码在使用浮动时工作正常,它向左或向右添加一个类,使得红色侧选择随机侧,黑色自动跟随,因为它是相对于彼此的。 Using absolute and fixed position changes that it has to add to both sides a class left or right to work. 使用绝对和固定位置更改,它必须向左侧或右侧添加一个类才能工作。 Someone know how to add this so when red is left, black is right and visa versa. 有人知道如何添加这个,所以当剩下红色时,黑色是正确的,反之亦然。

 // Random red & black // window.addEventListener('load', function() { // This is a ternary operator, which is just a shorthand way to // do an if/else statement. This basically says, if the random number // is less than .5, assign "left" to the scenario variable. // if it is greater (or equal to), assign "right" to the variable. var scenario = Math.random() < .5 ? "left" : "right"; document.querySelector(".red", ).classList.add("" + scenario); }); 
 .left { left: 0; } .right { right: 0; } .red, .black { width: 50%; height: 100%; } .black { position: absolute; background-color: black; } .red { position: fixed; background-color: red; } 
 <div class="red"></div> <div class="black"></div> 

Here is your solution: 这是你的解决方案:

 // Random red & black // window.addEventListener('load', function() { // This is a ternary operator, which is just a shorthand way to // do an if/else statement. This basically says, if the random number // is less than .5, assign "left" to the scenario variable. // if it is greater (or equal to), assign "right" to the variable. var scenario = Math.random() < .5 ? "left" : "right"; var scenario2 = scenario == "left" ? "right" : "left"; document.querySelector(".red", ).classList.add("" + scenario); document.querySelector(".black", ).classList.add("" + scenario2); }); 
 .left { left: 0;} .right { right: 0;} .red, .black { width: 50%; height: 100%; } .black { position: absolute; background-color: black; } .red { position: fixed; background-color: red; } 
 <div class="red"></div> <div class="black"></div> 

You should avoid absolute positioning in this case because you don't want overlapping here (it's not appropriate here). 在这种情况下你应该避免绝对定位,因为你不想在这里重叠(这里不合适)。 You can replace your CSS and JavaScript code just to apply single class for reversing. 您可以替换CSS和JavaScript代码,只是为了应用单个类进行反转。 Demo: 演示:

 // Random red & black window.addEventListener('load', function() { if (Math.random() < .5) document.body.classList.add("reversed"); }); 
 body { margin: 0; /* set height to be minimum of 100% of screen hieght */ min-height: 100vh; } body > div { width: 50%; } .red { background-color: red; position: fixed; left: 0; top: 0; bottom: 0; } .black { background-color: black; margin-left: 50%; min-height: 100vh; } body.reversed > .red { left: 50%; } body.reversed > .black { margin-left: 0; } 
 <div class="red"></div> <div class="black"></div> 

This is more of a hack than an answer. 这更像是一个黑客而不是一个答案。

You add padding-left:50% to .black and you give .red a higher z-index value to compensate for the difference in position settings. 你将padding-left:50%添加到.black ,你给.red一个更高的z-index值来补偿position设置的差异。

finally I added body {overflow:hidden} - body happens to be the container in this case - to tidy things up. 最后我添加了body {overflow:hidden} - 在这种情况下,body正好是容器 - 整理一下。

 // Random red & black // window.addEventListener('load', function() { // This is a ternary operator, which is just a shorthand way to // do an if/else statement. This basically says, if the random number // is less than .5, assign "left" to the scenario variable. // if it is greater (or equal to), assign "right" to the variable. var scenario = Math.random() < .5 ? "left" : "right"; document.querySelector(".red", ).classList.add("" + scenario); }); 
 body { margin: 0 auto; padding: 0; overflow: hidden } .left { left: 0; } .right { right: 0; } .red, .black { width: 50%; height: 100%; } .black { position: absolute; background-color: black; padding-left: 50% } .red { position: fixed; background-color: red; z-index: 2 } 
 <div class="red"></div> <div class="black"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM