I got quite a bit of help from another great user on this site already, am curious if someone could help with one more question on this code.
this is essentially where I am at.
one hovers the gray boxes to highlight an element on the screen. when one gray element is clicked a blue box appears, and the highlighted element stays as well. if the new blue element is clicked, the red box goes away and the blue box go away, and the hovers function accordingly after. if one element is active, and I want to click the other gray element, currently, another new element pops in, giving me two blue boxes. I only want the corresponding blue box active at a time. so if element 1 blue box is active, I want to click element 2, and deactivate blue box 1, while activating blue box 2.
here is I am currently at
html
<div class="bg">1</div>
<div class="bg2">1</div>
<div class="bgpopup">1</div>
<div class="bg">2</div>
<div class="bg2">2</div>
<div class="bgpopup">2</div>
css
.bg {
background: #ccc;
width: 200px;
height: 120px;
}
.bg:hover {
background: #000;
}
.bg2 {
position:absolute; top:250px;
left:250px;
background: red;
width: 200px;
height: 120px;
display:none;
}
.bgpopup {
background: blue;
width: 200px;
height: 120px;
display:none;
}
script
$(".bg").each(function(){
var $bg = $(this);
var $bg2 = $bg.nextAll('.bg2:first');
var $bgP = $bg.nextAll('.bgpopup:first');
$bg.data("activated", 0).hover(function(){
if($bg.data("activated") === 0) $bg2.toggle();
}).click(function(){
$bg.data("activated", 1);
$bgP.show().on("click", function(){
$bg.data("activated", 0);
$bg2.add( this ).hide();
});
});
});
with link http://jsfiddle.net/skinnyb/0vvk945y/8/
In that case and knowing that the HTML will not be changed in the future (cause the order is important!) I'd do it almost all using CSS's + next-sibling selector.
Regarding jQuery i'd keep it at the minimum and use it exclusively to toggle CSS classes:
var $bg = $(".bg"); // Cache all buttons
$(".bg").on("click", function(){
$bg.removeClass("active");
$(this).addClass("active");
});
$(".blue").on("click", function(){
$bg.removeClass("active");
});
.bg {
background: #ccc;
margin-top: 10px;
width: 200px;
height: 120px;
transition:0.3s;
}
.bg:hover {
background: #000;
}
.red {
position: absolute;
top: 250px;
left: 250px;
background: red;
width: 200px;
height: 120px;
visibility:hidden;
transition: 0.3s;
cursor:pointer;
opacity: 0;
}
.bg:hover + .red{
visibility: visible;
z-index:2; /* prioritize (overlay) hovered - over active ones*/
opacity: 1;
}
.bg.active + .red{
visibility: visible;
z-index:1; /* keep below hovered ones */
opacity: 1;
}
.bg.active + .red + .blue{
display: block;
}
.blue {
background: blue;
width: 200px;
height: 120px;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="bg">1</div>
<div class="red">1</div>
<div class="blue">1</div>
<div class="bg">2</div>
<div class="red">2</div>
<div class="blue">2</div>
PS: I've used other classNames to prevent headaches. Usually a popup shows at the screen center, not below some other element like you did... Feel free to edit my classes as you need.
Try hiding both blue's before one appears to ensure there is only one at a time:
jsfiddle.net/2sxy9mzw/
All I did was added one line before $bg.data("activated", 1);
which was:
$('.bgpopup').hide();
Related
I have three inline-block divs on my page (see JSFiddle):
Div #one contains a button 'Show' and is absolutely positioned so that it overlaps the div #two. When 'Show' is clicked, div #two slides out from under #one using translateX, like so:
When this happens, I would like to push div #three down so that it appears just below div #two, like so:
I'm not sure how to go about achieving this using pure CSS that doesn't involve moving #three along the Y-axis using #three { transform: translateY(...) }. I was wondering if translateX is the wrong approach here since it does not disturb the position of neighbouring elements, but I don't know what to use in its place.
As I have already stated in the comment section: It really depends on what your final goal is and what content you put in your divs - how everything is structured.
I feel like this is more of a XY-problem. I.e. the design-choice demands for a special case/solution that could be solved in another way so that the "hacky" solution does not have to exist in the first place.
Nevertheless, since you have asked for it I give you a solution for this specific problem:
const container = document.querySelector('.container');
const slide = document.getElementById('show');
const done = document.getElementById('hide');
const two = document.getElementById('two');
const right = document.querySelector('.right');
show.addEventListener('click', function() {
two.classList.add('show');
right.classList.add('shift');
});
hide.addEventListener('click', function() {
two.classList.remove('show');
setTimeout(function() {
right.classList.remove('shift');
}, 1000)
});
.left,
.right {
position: relative;
height: 200px;
margin-top: 5px;
display: inline-block;
border: 1px solid black;
float: left;
}
.left {
width: 100px;
}
.right {
width: 200px;
margin-left: 10px;
transform: translateX(0);
}
.right.shift {
clear: left;
display: block;
float: none;
transform: translateX(100px);
}
#one,
#two {
height: 100%;
}
#one {
background-color: lightblue;
position: absolute;
z-index: 1;
}
#two {
background-color: yellow;
transform: translateX(0);
transition: transform 1s;
}
#two.show {
transform: translateX(100%);
}
<div class="left">
<div id="one">
Click 'Show' to show panel 2
<button type='button' id='show'>Show</button>
</div>
<div id="two">
Click 'Hide' to hide panel 2
<button type='button' id='hide'>Hide</button>
</div>
</div>
<div class="right">Some other content</div>
Alternative
You could implement a spoiler section that you can toggle to display more information if it is desired.
const spoilerBtn = document.getElementById('spoiler-btn');
const spoiler = document.getElementById('spoiler');
spoilerBtn.addEventListener('click', function() {
spoiler.classList.toggle('show');
});
.left,
.right {
position: relative;
height: 200px;
margin-top: 5px;
display: inline-block;
border: 1px solid black;
float: left;
}
.left {
width: 100px;
}
.right {
width: 200px;
margin-left: 10px;
}
#spoiler {
background-color: tomato;
display: none;
}
#spoiler.show {
display: block;
}
<div class="left">Aside text here</div>
<div class="right">
Toggleable section: <button id="spoiler-btn">toggle</button>
<div id="spoiler">This content can be toggled</div>
<p>Some other content</p>
</div>
I am trying to make a div display when hovering over an a tag. I also want the div to not disappear when the mouse moves onto the displayed div. Although I want the div to disappear when you aren't hovering on the a tag text or the div itself.
This is being specifically used for a navigation bar that can display content on one of the tabs when hovering over it.
I have tried using the .div-name + .div-name2 {} method, but since I have content between these two divs, this solution does not work.
If there is a better way of doing this e.g. through some sort of bootstrap mechanic, that would be great to know as well as I am sure I am over-complicating this using javascript and jquery. Otherwise helping me with the javascript/jquery would be greatly appreciated.
$(".hover-btn").mouseenter(function() {
$(".hover-btn-section").stop().fadeIn(500);
});
$(".hover-btn").mouseleave(function() {
$(".hover-btn-section").stop().delay(500).fadeOut(500);
});
$(".hover-btn-section").mouseenter(function() {
$(".hover-btn-section").stop();
});
.hover-btn {
border: 1px solid red;
}
.hover-btn-section {
display: none;
width: 100%;
border: 1px solid green;
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class='hover-btn'>hover-btn</a>
<a href="#" class='link2'>link2</a>
<div class="hover-btn-section">
<p>Testing Area</p>
</div>
What you describe can be achieved using CSS only. Instead of the adjacent sibling selector + use the sibling selector ~:
.hover-btn {
border: 1px solid red;
}
.hover-btn:hover ~ .hover-btn-section {
opacity: 1;
pointer-events: all;
}
.hover-btn-section {
opacity: 0;
width: 100%;
border: 1px solid green;
background-color: yellow;
pointer-events: none;
transition: opacity .3s ease-in-out;
}
.hover-btn-section:hover {
opacity: 1;
pointer-events: all;
}
hover-btn
link2
<div class="hover-btn-section">
<p>Testing Area</p>
</div>
I am pretty new to jQuery and I am having a bit of difficulty adapting to it being a Java nerd.
I am trying to make these 3 boxes so that when you click one of them, it comes forward and the two in the back dim and stay there, in the back. The problem is that, I want to make it so when you click more than 1 box consecutively, the second box clicked doesn't come forward until the animation ends, much like a queue of box clicks. Right now it's all mixed up and the dimming is fine but the boxes come forward as soon as I click them and not when they should.
I tried callbacks and deferred to no avail.
Here is the code:
Javascript:
var zindex = 1;
$('.box_listener').click(function() {
$(this).css('z-index', zindex += 1);
$(this).siblings('.box_listener').fadeTo(3000, 0.5);
$(this).fadeTo(1, 1);
});
Here is the JSFiddle:
https://jsfiddle.net/asger/5yvvgoda/14/
var zindex = 1;
$('.box_listener').click(function() {
$(this).css('z-index', zindex += 1);
$(this).siblings('.box_listener').fadeTo(3000, 0.5);
$(this).fadeTo(1, 1);
});
#backgroundbox {
position: absolute;
width: 400px;
height: 200px;
background-color: #E5E8E8;
z-index: -5;
border-style: solid;
border-width: 1px;
}
.box_listener {
position: absolute;
width: 120px;
height: 120px;
background-color: white;
border-style: solid;
border-width: 1px;
}
#redbox {
left: 270px;
top: 20px;
border-color: red;
z-index: 0;
}
#bluebox {
left: 230px;
top: 60px;
border-color: blue;
z-index: 0;
}
#greenbox {
left: 210px;
top: 77px;
border-color: lightgreen;
z-index: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="backgroundbox">
<div class="box_listener" id="redbox">
</div>
<div class="box_listener" id="bluebox">
</div>
<div class="box_listener" id="greenbox">
</div>
</div>
Cheers and thanks!
A more bulletproof approach is to not use jQuery animations at all and instead use CSS transitions. The reason for this is twofold; CSS transitions can be automatically reversed and they can be GPU accelerated. It also means you don't have to artificially wait for the transition to complete before allowing user input.
To accomplish this, just set up two CSS classes; One that tells the elements you're going to animate how they should transition. The other class changes the values on the element, which causes the transition to happen. Then all jQuery needs to do is addClass() and removeClass() in order to cause the transitions to occur.
Below is an example of it in action. I've highlighted the most important aspects with comments.
$('.btn').on('click', function() {
// remove the active class from all buttons,
// this will reverse the transition
$('.btn').removeClass('active');
// apply it to only the current button clicked,
//this will start the transition
$(this).addClass('active');
});
.btn {
display: block;
width: 200px;
padding: 10px 20px;
margin-bottom: 5px;
background: cornflowerblue;
border: 0;
cursor: pointer;
/* set up a transition on any css transformations like
translating, scaling, rotating, etc. */
transition: transform 300ms ease-in-out;
}
/* when this class is added to the button it will scale it, but the
transition already on the button will make sure it happens slowly */
.active {
transform: scale(1.2);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Click the buttons</h2>
<button class="btn">First</button>
<button class="btn">Second</button>
<button class="btn">Third</button>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
How can I transition between two divs without seeing the background of the page during the transition. In this example, I would expect just to see the numbers change:
https://jsfiddle.net/j2td4hd7/15/
The transition easing is set to linear.
This doesn't have anything to do with the easing, it's that you're unnecessarily transitioning both elements.
It's easy to overcomplicate this -- instead think of the two images as a stack: the one on the bottom can stay visible the whole time, only the one on the top needs to fade in and out.
Compare below:
goodswap = function() {
$('#div2').fadeToggle(); // transitions the top element in and out
}
badswap = function() {
$('#div1, #div2').fadeToggle(); // transitions both elements in and out
}
.block-div {
position: absolute;
height: 200px;
width: 400px;
color: #fff;
font-size: 40px;
text-align: center;
}
.container { position: relative }
#div1 { background-color: red;}
#div2 { background-color: brown; display: none;}
#div3 { background-color: blue }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button onclick="goodswap()">Good fade</button>
<button onclick="badswap()">Bad fade</button>
<div class="container">
<div class="block-div" id="div3">(This is the page background)</div>
<div class="block-div" id="div1">1</div>
<div class="block-div" id="div2">2</div>
</div>
If the images are different sizes, this technique still works so long as the larger one is stacked with the higher z-index. To keep the rest of the page from jumping around, set the container's size to that of the larger image.
goodswap = function() {
$('#div2').fadeToggle(); // transitions the top element in and out
}
badswap = function() {
$('#div1, #div2').fadeToggle(); // transitions both elements in and out
}
.block-div {
position: absolute;
height: 200px;
width: 400px;
color: #fff;
font-size: 40px;
text-align: center;
}
.container { position: relative }
#div1 { background-color: red;}
#div2 { background-color: brown; width: 500px; display: none;}
#div3 { background-color: blue }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button onclick="goodswap()">Good fade</button>
<button onclick="badswap()">Bad fade</button>
<div class="container">
<div class="block-div" id="div3">(This is the page background)</div>
<div class="block-div" id="div1">1</div>
<div class="block-div" id="div2">2</div>
</div>
If you have two elements with black background and opacity of 50% on each of them - they are not combined into 100% black :)
Check the following:
div {
background: black;
width: 150px;
height: 150px;
position: absolute;
}
div.half {
opacity: 0.5;
left: 0;
top: 0;
}
div.full {
top: 0;
left: 150px;
}
<div class="half"></div>
<div class="half"></div>
<div class="full"></div>
During the animation - the opacity of the two elements is changing from 0 to 100 and from 100 to 0, but the background color is not "combined".
I think the following will provide what you are looking for:
window.swap = function(divId){
if(divId === 'div1'){
jQuery('#div1').css('zIndex', 1);
jQuery('#div2').css('zIndex', 2);
jQuery('#div2').fadeIn({easing: 'linear', complete: function() {
jQuery('#div1').fadeOut({easing: 'linear'})
} }
);
}else{
jQuery('#div2').css('zIndex', 1);
jQuery('#div1').css('zIndex', 2);
jQuery('#div1').fadeIn({easing: 'linear', complete: function() {
jQuery('#div2').fadeOut({easing: 'linear'})
}
});
}
}
.block-div {
position: absolute;
height: 200px;
width: 400px;
color: #fff;
font-size: 40px;
text-align: center;
}
#div2{ display: none; background-color: brown; }
#div1{ background-color: red; }
html, body{
background-color: #fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="block-div" onclick="swap('div1')" id="div1">1</div>
<div class="block-div" onclick="swap('div2')" id="div2">2</div>
Note regarding comments from the OP:
In case you want the same effect with two elements that has different height, you can create two dummy div, one for each div you have. The dummy1 will be in the size of div1, but contain the bg of div2, and same for dummy2. now you can use these dummy divs to do the transition.
Mozilla.com has this tab on the top of their site that you can click and a menu drops down. I have a client who wants me to do the same thing but upside down, from the bottom half of the page. Apparently this is a really hard request. How do I make something like tabzilla that goes up and either overlaps or pushes the content away? Thanks!
Update: I love you guys.
Edit: http://hemakessites.com/mayukh/4/ Why does the top "Sign In/Register" pop down and the "Toggle" on the bottom pops up? I'm not seeing the difference besides 'top' and 'bottom' in the css. How does that change the direction of the popup?
Also, clicking the '337-9147' will expand the menu. I only want the button region to be clickable. How can I accomplish this?
You guys are awesome and I'm going to return the favor by answering some questions on here when I get time.
I took a similar approach as others, in that you set a div to have a fixed, or absolute position at the bottom of the screen (depending on whether the tab should always be visible, or only at the very bottom). Then, you can write some very simple javascript to vary the height of the element, and as the bottom is fixed, it will cause the tab to rise into the screen.
Essentially all you need is
.container{
position: absolute;
bottom: -1px;
}
And
$('.container').toggle(function(){
$(this).animate({height:'205px'}, 500)
},function(){
$(this).animate({height:'20px'}, 200)
});
Here's a jsfiddle demo.
Here's a jQuery solution, which is smoother than css3:
So, you'll want to do something like this jsfiddle (NOTE: This requires jQuery):
http://jsfiddle.net/cFkn2/
$(document).ready(function() {
$('#tab').click(function() {
if ($('#tab').css('height') == '20px') {
$('#tab').animate({
height: '100px'
}, 1000);
}
else {
$('#tab').animate({
height: '20px'
}, 1000);
};
});
});
and
#tab{
position: absolute;
bottom: 0;
width: 100%;
background-color: red;
height:20px;
}
and
<div id="tab">CONTENT</div>
Style, edit, and add easing to taste.
I was lazy to make here click handler, so it is css3 only hover sample
I used fixed position with {top: 100%}, transition for animation, margin <0 to show;
HTML
<div id="menu">
<div id="handler">handler</div>
<div id="menucontent">
menu menu<br>
menu menu<br>
</div>
</div>
<div id="content">
<div> text text text</div>
<div> text text text</div>
<!-- many of them -->
<div> text text text</div>
<div> text text text</div>
<div> text text text</div>
</div>
CSS:
#content > div {
font-size: 2em;
height: 2.1em;
border: 1px solid black;
margin-top: 10px;
}
#menu {
left: 30px;
position: fixed;
font-size: 20px;
width: 300px;
height: 300px;
top: 100%;
border: 1px solid red;
background: white;
-webkit-transition: all 1s;
-mozilla-transition: all 1s;
-o-transition: all 1s;
transition: all 1s;
}
#menu #handler {
position: absolute;
top: -40px;
background: green;
font-size: 30px;
height: 40px;
padding-left: 5px;
padding-right: 5px;
left: 10px;
}
#menu:hover {
margin-top: -300px;
}
with click, or
JS:
$(function() {
$('#menu #handler').click(function() {
$('#menu').toggleClass('shown');
});
});
in css change hover to class shown
#menu.shown {
margin-top: -300px;
}