/    Sign up×
Community /Pin to ProfileBookmark

Overlaying DIV elements (of Over-lapping DIVs)

My attempt to simulate a roll-a-dex card pile is below.
What it is designed to do and what it does are dynamic opposites.

[code=php]
<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″ />
<title> Rolex Cards </title>
<style type=”text/css”>
.cards { position:absolute;
top:200px; left:200px;
width:30%; height:30%;
border:1px solid red; z-index:1; }
</style>
</head>
<body>
<div id=”contents”>
<div class=”cards”>1</div>
<div class=”cards”>2</div>
<div class=”cards”>3</div>
<div class=”cards”>4</div>
<div class=”cards”>5</div>
</div>

<script type=”text/javascript”>
function init () {
var colors = [‘red’,’orange’,’green’,’blue’,’magenta’];
var sel = document.getElementById(‘contents’).getElementsByClassName(‘cards’);
for (var i=0; i<sel.length; i++) {
sel[i].top = (200 – (20*i))+’px’;
sel[i].left = (200 + (20*i))+’px’;
sel[i].zIndex = 1 + (i*2);
sel[i].borderColor = colors[i];
}
} init();

/* should give following (but does not)
|———–|
|5 |
|———–| |
|4 |–|
|———–| |
|3 |–|
|———–| |
|2 |–|
|———–| |
|1 |–|
| |
|———–|
*/
</script>
</body>
</html>
[/code]

Any idea why I’m not getting expected? Should I use ‘relative’ rather than ‘absolute’? (tried that with worse displayed output)

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERauthorJan 10.2016 — Found part of the problem.

[code=php]
sel[i].style.top = (200 - (20*i))+'px';
sel[i].style.left = (200 + (20*i))+'px';
sel[i].style.zIndex = 1 + (i*2);
sel[i].style.borderColor = colors[i];
[/code]

Forgot the .style. in the definitions.
Copy linkTweet thisAlerts:
@JMRKERauthorJan 10.2016 — Question now is how to define the <div> so that it covers lower z-index displays.

Transparency does not seem to work if images are not involved.

I plan to roll the displays to the top by altering the z-index values for the display desired.

[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Rolex Cards </title>
<style type="text/css">
.cards { position:absolute;
top:200px; left:200px;
width:30%; height:30%;
border:1px solid red; z-index:1; }
</style>
</head>
<body>
<div id="contents">
<div class="cards">1<p>A</div>
<div class="cards">2<p>B</div>
<div class="cards">3<p>C</div>
<div class="cards">4<p>D</div>
<div class="cards">5<p>E</div>
</div>

<script type="text/javascript">
function init () {
var colors = ['red','orange','green','blue','magenta'];
var sel = document.getElementById('contents').getElementsByClassName('cards');
for (var i=0; i<sel.length; i++) {
sel[i].style.top = (200 - (25*i))+'px';
sel[i].style.left = (200 + (25*i))+'px';
sel[i].style.zIndex = 1 + i;
sel[i].style.borderColor = colors[i];
}
} init();

/* should give following (but does not)
|-----------|
|5 |
|-----------| |
|4 |--|
|-----------| |
|3 |--|
|-----------| |
|2 |--|
|-----------| |
|1 |--|
| |
|-----------|
*/
</script>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@JMRKERauthorJan 10.2016 — Getting closer by using a .style.backgroundColor definition. :rolleyes:

[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Rolex Cards </title>
<style type="text/css">
.cards { position:absolute;
top:200px; left:200px;
width:30%; height:30%;
border:1px solid red; z-index:1; }
</style>
</head>
<body>
<div id="contents">
<div class="cards">1<p>A</div>
<div class="cards">2<p>B</div>
<div class="cards">3<p>C</div>
<div class="cards">4<p>D</div>
<div class="cards">5<p>E</div>
</div>

<script type="text/javascript">
function init () {
var colors = ['red','orange','lime','cyan','magenta'];
var sel = document.getElementById('contents').getElementsByClassName('cards');
for (var i=0; i<sel.length; i++) {
sel[i].style.top = (200 - (25*i))+'px';
sel[i].style.left = (200 + (25*i))+'px';
sel[i].style.zIndex = 1 - i;
sel[i].style.borderColor = 'black';
sel[i].style.backgroundColor = colors[i];
}
} init();

/* should give following (but does not)
|-----------|
|5 |
|-----------| |
|4 |--|
|-----------| |
|3 |--|
|-----------| |
|2 |--|
|-----------| |
|1 |--|
| |
|-----------|
*/
</script>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@JMRKERauthorJan 10.2016 — OK, should have thought it through more before posting, but here is my question and my answer for others who might be interested in my ramblings!

[code=php]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title> Rolex Cards </title>
<style type="text/css">
.cards { position:absolute;
top:200px; left:200px;
width:30%; height:30%;
padding: 0.25em 1em;
border:1px solid red; z-index:1;
overflow-y:auto;
}
</style>
<base href="https://www.nova.edu/portal/optometry/otm/pics/4fun/" target="_blank">
</head>
<body>
<div id="contents">
<div class="cards">1<p>Exhausted<p><img src="11.jpg" height="100px"; width="75px" align="right"></div>
<div class="cards">2<p>Angry<p><img src="21.jpg" height="100px"; width="75px" align="right"></div>
<div class="cards">3<p>Embarased<p><img src="31.jpg" height="100px"; width="75px" align="right"></div>
<div class="cards">4<p>Enraged<p><img src="41.jpg" height="100px"; width="75px" align="right"></div>
<div class="cards">5<p>Overwhelmed<p><img src="51.jpg" height="100px"; width="75px" align="right"></div>
</div>

<script type="text/javascript">
function rollup(info) {
var sel = document.getElementById('contents').getElementsByClassName('cards');
for (var i=0; i<sel.length; i++) { sel[i].style.zIndex = 1 - i; }
info.style.zIndex = 5;

}
function init () {
var colors = ['red','orange','lime','cyan','magenta'];
var sel = document.getElementById('contents').getElementsByClassName('cards');
for (var i=0; i<sel.length; i++) {
sel[i].style.top = (200 - (25*i))+'px';
sel[i].style.left = (200 + (25*i))+'px';
sel[i].style.borderColor = 'black';
sel[i].style.backgroundColor = colors[i];
sel[i].style.zIndex = 1 - i;
sel[i].onclick = function() { rollup(this); }
}
} init();

</script>
</body>
</html>
[/code]

Other solutions are welcome for discussion.
Copy linkTweet thisAlerts:
@Ay__351_eJan 11.2016 —  <br/>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;style&gt;
/* http://www.w3schools.com/css/css_dropdowns.asp */
.bir {left:30px; top:210px; background-color:red; color:green; opacity:0.75;}
.iki {left:60px; top:180px; background-color:orange; color:blue; opacity:0.75;}
.uc {left:90px; top:150px; background-color:yellow; color:purple; opacity:0.75;}
.dort {left:120px; top:120px; background-color:green; color:red; opacity:0.75;}
.bes {left:150px; top:90px; background-color:blue; color:orange; opacity:0.75;}
.alti {left:180px; top:60px; background-color:darkblue; color:orange; opacity:0.75;}
.yedi {left:210px; top:30px; background-color:purple; color:yellow; opacity:0.75;}

.bir:hover { opacity:1; z-index:1; width:320px; height:200px;}
.iki:hover { opacity:1; z-index:1; width:320px; height:200px;}
.uc:hover { opacity:1; z-index:1; width:320px; height:200px;}
.dort:hover { opacity:1; z-index:1; width:320px; height:200px;}
.bes:hover { opacity:1; z-index:1; width:320px; height:200px;;}
.alti:hover { opacity:1; z-index:1; width:320px; height:200px;}
.yedi:hover { opacity:1; z-index:1; width:320px; height:200px;}


.kart {
position: absolute;
display: inline-block;
width:160px; height:100px;
font-size:15px;
}

.kart-content {
/* display: none;*/
position: absolute;
/* background-color: #f9f9f9;*/
min-width: 100px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
padding: 12px 16px;
}

.kart:hover .kart-content {
/* display: block;*/

}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="kart yedi"&gt;
&lt;span&gt;7&lt;/span&gt;
&lt;div class="kart-content"&gt;
&lt;p&gt;7&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="kart alti"&gt;
&lt;span&gt;6&lt;/span&gt;
&lt;div class="kart-content"&gt;
&lt;p&gt;6&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="kart bes"&gt;
&lt;span&gt;5&lt;/span&gt;
&lt;div class="kart-content"&gt;
&lt;p&gt;5&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="kart dort"&gt;
&lt;span&gt;4&lt;/span&gt;
&lt;div class="kart-content"&gt;
&lt;p&gt;4&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="kart uc"&gt;
&lt;span&gt;3&lt;/span&gt;
&lt;div class="kart-content"&gt;
&lt;p&gt;3&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="kart iki"&gt;
&lt;span&gt;2&lt;/span&gt;
&lt;div class="kart-content"&gt;
&lt;p&gt;2&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="kart bir"&gt;
&lt;span&gt;1&lt;/span&gt;
&lt;div class="kart-content"&gt;
&lt;p&gt;1&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@rootJan 12.2016 — Curious why you ... document.getElementById('contents').getElementsByClassName('cards'); ... when I would have thought that
... document.getElementsByClassName('cards'); ... would be sufficient. I removed it and ran the code in my browser and it did just the same as with the .getElementById('contents') in place when removed from the code.
Copy linkTweet thisAlerts:
@JMRKERauthorJan 13.2016 — Curious why you ... document.getElementById('contents').getElementsByClassName('cards'); ... when I would have thought that
... document.getElementsByClassName('cards'); ... would be sufficient. I removed it and ran the code in my browser and it did just the same as with the .getElementById('contents') in place when removed from the code.[/QUOTE]


In my first iterations of the script I thought about using the 'contents' block as the container

to allow movement of ALL the cards at one time should I need to change the CSS position attribute to 'relative'.

It was just carried over to the posted version and as you noted, it can be used just a effectively

without the extra bit of code if the positioning is satisfactory.
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...