So I'm trying to create an animation on a webpage and am trying to figure out a way to do it using CSS3, but am quite confused as to how I can do it.
What I need to have happen is when users click on a link element I want a div to expand and be populated with content specific to the link element clicked. For example, when a user clicks on a link titled "About", a div below the link element will expand and have some content appear. THEN, when they click another link, say "Contact", the content specific to "About" will disappear and content specific to "Contact" will appear as the div re-sizes to fit the new content.
I think I can do this pretty easily with Javascript, but can someone tell me if it might be easier to do/possible with CSS3?
Thanks all.
As already mentioned, JavaScript is your best friend for this. But since you asked if it would be possible with CSS3 I had to give it a try. Basically what I’ve done is I’ve used the target selector to trigger the animation. So when you click a link, a div expands with some content and if you click another link a new div, with some new content (positioned in the same place) expands, creating the illusion that it’s the same div expanding.
It’s not an optimal solution and I made this example really quick so it’s not working exactly as you wanted, but it gives you at least a picture on how it could be done with just CSS.
Hope that helps!
Here's a demo and here's the code from my example:
HTML
Box<br />Box two
<div id="box">Hello</div>
<div id="boxtwo">Hello again,</div>
CSS
#box, #boxtwo{
position: absolute;
top: 50px;
left: 50px;
width: 0px;
height: 0px;
background-color: #e3e3e3;
color: transparent;
}
#box:target {
-webkit-animation: expand 1.0s ease-in forwards;
}
#boxtwo:target {
-webkit-animation: expand 1.0s ease-in forwards;
}
#-webkit-keyframes expand {
0% {width: 0px; height: 0px; color: transaprent;}
50% {width: 100px; height: 100px; color: transparent;}
100% {width: 100px; height: 100px; color: #000000;}
}
The simplest way for a click to trigger an animation is to add a CSS class to an object upon the click and have an CSS3 transition or animation configured for any object with that class.
Your second class to hide the item can then remove that class name from the same object.
All the details of the animation/transition would be specified in CSS3 style rules. Only the add/remove of the class name would be done with javascript.
CSS3 all by itself can trigger animations/transitions with the :hover pseudo selector, but isn't a lot more capable than that and can't trigger an animation based on a click.
I don't think this is a CSS3 vs. JavaScript question. Even if you use CSS3 for the animations, you're likely to need JavaScript to trigger the animations based on a click event.
Based on what you need to do, I see a couple of main options:
As #jfriend00 said, add or remove CSS classes which perform the animation.
Use jQuery's show, hide, fadeIn, fadeOut, and animate APIs.
What you need is some juery to spice up whatever you are developing... If am not wrong you want some thing like this: CSS3 vs Jquery
Get the jquery library and reference it in your page.
here is a snippet to jump start you.
<a id="home" href="home.html">Home</a>
<a id="about" href="about.html">About</a>
<div id="home_div"></div>
<div id="about_div"></div>
<script type="text/javascript">
$('#home').click(function () {
$('html').animate({ scrollTop: 500 }, 1000);
$('#home_div').animate().show('slow');
$('#about_div').animate().fadeOut('slow');
return false;
});
$('#about').click(function () {
$('html').animate({ scrollTop: 500 }, 1000);
$('#home_div').animate().fadeOut('slow');
$('#about_div').animate().show('slow');
return false;
});
</script>
You can change the effects to other available ones.
Related
I've tried searching for a solution to this problem, but haven't found one yet.
What I'm trying to do is simple:
When I click one button, I'd like a box to move 200px to the right with CSS transitions. When I click a second button, I'd like the box to move 200px down from the position it is currently in.
I have this basic code here:
HTML
<button class="one">First</button>
<button class="two">Second</button>
<div class="box"></div>
CSS
.box {
width: 100px;
height: 100px;
background-color: red;
transition: transform 2s;
}
.box.transOne {
transform: translateX(200px);
}
.box.transTwo {
transform: translateY(200px);
}
JS
$(".one").click(function() {
$(".box").toggleClass("transOne");
});
$(".two").click(function() {
$(".box").toggleClass("transTwo");
})
However, when I click on button number two, the box does move 200 down, but it moves diagonally back to the first X axis position while it's going down (I.e. it doesn't stay 200px over on the X axis).
Is there a way I can possibly do this with keyframes? Like triggering a second keyframe with a second button click, etc. Or is there a better way? I'm pretty stumped and can't find any solutions, so any help is much appreciated. Thanks!
SHORT ANSWER
set the translation X in class .transTwo too
.box.transTwo {
transform: translate(200px 200px);
}
EXPLANATION
the transform is overriding the others, this is the nature behaviour of the css, just like other property color, background-color,
The basic rule is the latest property set is the strongest, the strongest is at inline style unless you implement !important
I know this probably cannot be done without javascript but I dont mind using it.
I have a div that grabs the width percentage with PHP.
All I need to do is that on page load I would like the bar ( Div ) to start at 0px width and transition to the correct width percentage.
I can do it fine of course on a Hover or Active state but cant seem to get it right with page load.
Do you use jQuery? Theres a built-in document ready function. Inside of that, add a class to the div. Then in your CSS you can style the two different states of the div to accommodate the transition
Javascipt
$(function(){
$('.bar').addClass('.ready');
});
CSS
.bar {
width: 0;
transition: width 500ms;
}
.bar.ready {
width: 100%;
}
I've a <p> tag and when it starts sliding from left to right it leaves a kind of trail through the screen. I've never seen anything like this before.
I'm animating it using jQuery:
$(document).ready(function(){
$(".choosenHero").animate({
"right": "300px"
}, 3000);
});
and here is the screenshot:
You might try using simple CSS transitions rather than jquery. As jquery does animation by changing css properties and is not the same thing.
So use jquery to add a class which has the end state of say:
.moved{
left: 300px;
}
and on the initial state of the paragraph use something like:
.moving-object{
position:relative;
transition: left 2s;
left: 0px;
}
Example:
<div id="one">
(content)
</div>
<div id="two">
<ul>............</ul>
</div>
I want to create an effect that appears that #two is comming down from #one, I tried using transitions so when I :hover over #one so #two would appear to be coming down from #one but the content stayed there while only the background changed in size, I want the whole list to appear to be coming down from #one like in this website: http://merryweddings.com/
If you know the size of the div that will pop up, you can do a simple transition on the 'height' property, like this:
http://jsfiddle.net/BeDQr/
You also could use the transition on the 'max-height' property and set it to a very large value.
#two {
max-height: 0;
overflow: hidden;
transition-property: max-height;
transition: all 1s ease-in-out;
}
.wrapper:hover #two {
max-height: 500px;
}
But in this case, the end of the animation might be a bit abrupt.
You might want to use JQuery for this instead of pure CSS.
Check out this example : http://labs.abeautifulsite.net/jquery-dropdown/
This link also shows a lot of possible JQuery solutions.
See this link here: How can I transition height: 0; to height: auto; using CSS?
Also see the link in the first answer of the above link which is here: Can you use CSS3 to transition from height:0 to the variable height of content?
This, unfortunately is the only solution you have for a pure CSS method. The second link shows a sort of workaround or a hack. The first gives some further details.
So I have this modal that pops up using the CSS :target selector. However, the page jumps to the anchor when clicked. I would like to prevent the page from jumping to the :target selector. How can I do this?
Info
<div id="openModal" class="modalDialog">
CSS:
.modalDialog {
position: absolute;
pointer-events: none;
z-index: 99999;
opacity:0;
}
.modalDialog:target {
opacity:1;
pointer-events: auto;
}
.modalDialog > div {
width: 900px;
height: 506px;
position: relative;
background: rgba(0,0,0,0.9);
}
Make the .modalDialog position: fixed instead of absolute. This will cause it to always be positioned at wherever the page is currently scrolled.
A more complete example: http://codepen.io/mblase75/pen/xbRNeV
(There's some other trickery involved in that codepen demo -- adding another target to the 'close' button on the modal which is also fixed keeps the page from scrolling when the modal is closed, and changing the z-index of your modal from -1 to 100 (or some other suitably large integer) will keep it from blocking clicks right after you close it.)
Use
...
in order to change :target selector in CSS but go nowhere. The fragment #/ doesn't exists, so page won't scroll, but the :target selector will change to affect CSS change.
try stopping the default action of the anchor tag
$('a').on('click', function(e){
e.preventDefault();
//do your poppin' up here.
});
This has nothing to do with CSS actually, it's plain old HTML.
You have a hash id in your link, it references an element on the page. Every browser would scroll the page to the referenced element when such link is clicked. That's very standard behavior.
You can't prevent that other than by not using this technique. Well, maybe there is a way to prevent scrolling with JavaScript black magic, but you shouldn't.
Use some jQuery instead: http://docs.jquery.com/Tutorials:Basic_Show_and_Hide
Have you tried the css3 :focus selector?
https://developer.mozilla.org/en-US/docs/Web/CSS/:focus
Also
CSS Menu - Keep parent hovered while focus on submenu
You can also use a <button> tag instead and style it however you please. That would prevent your page jumping since it is not an anchor.