CSS transitions not effecting HTML generated by Javascript - javascript

I have a css transform that rotates objects on hover.
.rotate { transition: 0.2s; }
.rotate:hover { transform: rotate(20deg); }
This works perfectly fine when applied to a hard coded HTML element.
However the page contains a script that generates more HTML and inserts it into a div (div.innerHTML = newHTML).
The css transitions do not work on any elements generated by the script.
Why would this be the case and how can it be corrected?
EDIT:
Here is a simplified example that shows my code, however I'm unable to get the example to work at all, the hard coded rotate and the generated html don't rotate and I have no idea why.
<!doctype html>
<head>
<style>
.rotate {
transition: 0.2s;
}
.rotate:hover {
transform: rotate(20deg);
}
</style>
</head>
<body>
<a class="rotate" href="#">A test text</a>
<div id="plot"></div>
<script>
function makeHTML()
{
return '<a class="rotate" href="#">I am a HTML link</a>';
}
var plot = document.getElementById('plot');
var text = makeHTML();
plot.innerHTML = text;
</script>
</body>
</html>

transform only applies to block-level elements. By default, <a> elements have display:inline. So you need to apply display: inline-block;, for the transform to work:
function makeHTML() {
return '<a class="rotate" href="#">I am a HTML link</a>';
}
var plot = document.getElementById('plot'),
text = makeHTML();
plot.innerHTML = text;
.rotate {
transition: 0.2s;
display: inline-block;
}
.rotate:hover {
transform: rotate(20deg);
}
<a class="rotate" href="#">A test text</a>
<div id="plot"></div>

Related

How do I fade 2 images with jQuery on mouseenter and mouseleave?

I have a custom button (code below). I want it to:
rotate quickly 360 on mouseenter (currently working fine)
fade quickly to a darker image on mouseenter (also currently working fine)
NOT un-rotate on mouseleave (currently working fine)
I can't yet figure out how to:
fade back to the original image on mouseleave (not working yet)
I have tried so many variations of jQuery including .hover, .fadeToggle, fadeIn, fadeOut as well as animate but none have seemed to work for me.
Am I missing something really simple and obvious?
NOTE: I have just used the Apple logo for demonstration here. If I can get the 'fade back on mouseleave' working I can just transfer it to my real life situation.
var thevalue = 1;
$("div.main").mouseenter(function() {
thevalue = thevalue + 1;
if (thevalue % 2 == 0) {
$(this).addClass("myopacity");
} else {
$(this).removeClass("myopacity");
}
$(this).addClass("change").delay(500).queue(function() {
$(this).removeClass("change").dequeue();
});
});
div.main {
width: 100px;
height: 100px;
}
div.main img {
width: 100%;
height: 100%;
}
.change {
-ms-transform: rotate(360deg);
/* IE 9 */
-webkit-transform: rotate(360deg);
/* Chrome, Safari, Opera */
transform: rotate(360deg);
transition-duration: .5s;
}
.myopacity {
opacity: 0.6;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="main">
<img src="https://image.freepik.com/free-icon/apple-logo_318-40184.jpg">
</div>
<p id="dis"></p>
</body>
</html>
Thanks in advance!
is this what you want. hope this will help to you.
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<style type="text/css">
div.main{
width: 100px;
height: 100px;
}
div.main img{
width: 100%;
height: 100%;
}
.change{
-ms-transform: rotate(360deg); /* IE 9 */
-webkit-transform: rotate(360deg); /* Chrome, Safari, Opera */
transform: rotate(360deg);
transition-duration: 5s;
}
</style>
<body>
<div class="main">
<img src="https://image.freepik.com/free-icon/apple-logo_318-40184.jpg">
</div>
<p id="dis"></p>
<script type="text/javascript">
var thevalue = 1;
$("div.main").mouseenter(function(){
$(this).addClass("change").fadeTo('fast',0.7).delay(5000).queue(function(){
$(this).removeClass("change").fadeTo('slow',1.0).dequeue();
});
});
</script>
</body>
</html>
I'm not 100% certain what you are after... I think this is close. Rotates 360° and opacity dims to 60%, then rotates back to 0° and full opacity.
No clue why you even needed the opacity class or the associated jQuery for it.
$("div.main").hover(
function() {
$(this).addClass('change').addClass('myopacity');
},
function() {
$(this).removeClass('myopacity')
});
body { padding: 40px; }
div.main {
width: 100px;
height: 100px;
opacity: 1;
transition: all .5s;
transition: all .5s;
background: url(https://image.freepik.com/free-icon/apple-logo_318-40184.jpg) no-repeat center center;
background-size: cover;
}
div.main img {
width: 100%;
height: 100%;
}
.main.change {
-ms-transform: rotate(360deg);
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
transition: all .5s;
background: url(https://image.freepik.com/free-icon/windows-8-logo_318-40228.jpg) no-repeat center center;
background-size: cover;
}
.change.myopacity {
opacity: .6; }
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="main">
</div>
<p id="dis"></p>
</body>
</html>
If you want the images in the actual HTML, then you can use the jQuery hover function to alter the image sources.
Found it.
Trying to manage two transitions on the exact same element was making it too complicated.
I ended up having to add one class to the img element and another to the div element. The img element now manages the rotation and the div element manages the fade through simple CSS :hover transitions.
This makes the jQuery much more simple.
See updated code below:
$("div.main").mouseenter(function() {
$(".image").addClass("change").delay(500).queue(function() {
$(".image").removeClass("change").dequeue();
});
});
// jQuery now much more simple. No need for variables or the if/else statement
div.main {
width: 100px;
height: 100px;
-webkit-transition: all .5s ease-in;
-webkit-transition: all .5s ease-out;
-moz-transition: all .5s ease-in;
-moz-transition: all .5s ease-out;
-o-transition: all .5s ease-in;
-o-transition: all .5s ease-out;
}
/* This will take care of the fade transition on :hover */
div.main img {
width: 100%;
height: 100%;
}
.change {
-ms-transform: rotate(360deg);
/* IE 9 */
-webkit-transform: rotate(360deg);
/* Chrome, Safari, Opera */
transform: rotate(360deg);
transition-duration: .5s;
}
/* .myopacity {
opacity: 0.6;
} */
/* The .myopacity class is no longer necessary as it's taken care of through the div.main:hover class below */
div.main:hover, div.main:active, div.main:focus {
opacity: 0.6;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div class="main">
<img src="https://image.freepik.com/free-icon/apple-logo_318-40184.jpg" class="image">
</div>
<p id="dis"></p>
</body>
</html>
Kind of cheated with not using jQuery for the fade transition (as originally hoped for) but this works equally as well!

CSS Transitions not working when class is changed by javascript called from child element

I've created a modal window that fades in and out when you click an anchor. The HTML for this is very simple:
<div id="main_wrapper" class="displayed">
//Some content here
</div>
<div id="dynamic">
//Dynamically generated (AJAX) content here
//the below anchor skips the CSS transition somehow!
<a href='#' onclick='toggleDynamic(); return false;'>Cancel</a>
</div>
//This anchor works exactly as intended
<a href='#' onclick='toggleDynamic(); return false;'>Dynamic</a>
The CSS is just slightly more complex, and I've left out some properties for the sake of brevity:
.displayed{
opacity: 1 !important;
pointer-events: auto !important;
transition: opacity 1s ease-in .5s !important;
}
#main_wrapper{
max-width: 800px;
opacity: 0;
pointer-events: none;
transition: opacity 1s linear;
}
#dynamic{
position: fixed;
display: block;
opacity: 0;
pointer-events: none;
max-width: 800px;
transition: opacity 1s linear;
}
Then I simply use a toggling javascript function:
function $I(id) {
return document.getElementById(id);
}
var dynamic = $I('dynamic');
var main = $I('main_wrapper');
function toggleDynamic() {
if(dynamic.className === "") {
dynamic.className = "displayed";
main.className = "";
} else {
dynamic.className = "";
main.className = "displayed";
}
}
ALL of the above code works exactly as expected except one thing - when the anchor inside #dynamic is clicked, the div disappears without a transition! I can't make sense of this, as you can click on the outside anchor all day and get a nice fading transition.
This occurs in all browsers as far as I can tell (IE, FF, Chrome, Opera).
EDIT: I've changed the javascript as it wasn't properly addressing the className before.
I can't see why your JS would work as it is, since you're not modifying your #dynamic class, but just a variable (dynamic). For me the corrected snippet works like a charm.
function $I(id) {
return document.getElementById(id);
}
var dynamic = $I('dynamic').className;
var main = $I('main_wrapper').className;
function toggleDynamic() {
if($I('dynamic').className === "") {
$I('dynamic').className = "displayed";
main = "";
} else {
$I('dynamic').className = "";
main = "displayed";
}
}
.displayed{
opacity: 1 !important;
pointer-events: auto !important;
transition: opacity 1s ease-in .5s !important;
}
#main_wrapper{
max-width: 800px;
opacity: 0;
pointer-events: none;
transition: opacity 1s linear;
}
#dynamic{
position: fixed;
display: block;
opacity: 0;
pointer-events: none;
max-width: 800px;
transition: opacity 1s linear;
}
<div id="main_wrapper" class="displayed">
//Some content here
</div>
<div id="dynamic">
//Dynamically generated (AJAX) content here
//the below anchor skips the CSS transition somehow!
<a href='#' onclick='toggleDynamic(); return false;'>Cancel</a>
</div>
//This anchor works exactly as intended
<a href='#' onclick='toggleDynamic(); return false;'>Dynamic</a>

javascript function mOver(obj) application

Hi: I'm doing a site in wordpress and I need two pieces of code that keep giving me hard time. I know is easy but I can't make it work.
I need to make that when mouse over (or enter) on any of three image, the image change (slide) into another image with descriptive text. How can I include the images onto this sample code and as per image on link bellow?
<div onmouseover="mOver(this)" onmouseout="mOut(this)" style="background-color:#D94A38;width:120px;height:20px;padding:40px;">Mouse Over Me</div>
<script>
function mOver(obj)
{
obj.innerHTML="Thank You"
}
function mOut(obj)
{
obj.innerHTML="Mouse Over Me"
}
</script>
Mouse over change content http://www.nrgtechnologies.com.au/moreimages/plain-2.jpg
Also very similar effect with a horizontal menu with sliding transition between 3 or 4 menu elements on mouseover as per image on link bellow.
mouse over sliding menu and content: http://www.nrgtechnologies.com.au/moreimages/plain-1.jpg
Thanks for the help
Please look at below script:
<!DOCTYPE HTML>
<html>
<head>
<title>Test</title>
<style type="text/css">
.slide {
background-image: url(plain-2.jpg);
width: 295px;
height: 220px;
-webkit-transition: background-position .5s ease-in-out;
}
.slide-1 {
background-position: -30px -215px;
-webkit-transition: background-position .5s ease-in-out;
}
.slide-2 {
background-position: -354px -215px;
-webkit-transition: background-position .5s ease-in-out;
}
.slide-3 {
background-position: -675px -215px;
-webkit-transition: background-position .5s ease-in-out;
}
.moz-over {
background-color: #ccc;
}
</style>
</head>
<body>
<div id="images" class="slide slide-1">
</div>
<span onmouseover="mozOver(this)" onmouseout="mozOut(this)">slide-1</span>
<span onmouseover="mozOver(this)" onmouseout="mozOut(this)">slide-2</span>
<span onmouseover="mozOver(this)" onmouseout="mozOut(this)">slide-3</span>
<script type="text/javascript">
var mozOver = function(target) {
target.className = "moz-over";
var images = document.getElementById("images");
images.className = "slide " + target.innerHTML;
};
var mozOut = function(target) {
target.className = "";
};
</script>
</body>
</html>
I am not sure whether is you want. I the logic is like above.
Add an id attribute to your div and remove the javascript handlers:
<div id="description"></div>
Then add your handlers to your images instead:
<img onmouseover="mOver(this)" onmouseout="mOut(this)" ... />
Then in your functions use something like this:
function mOver(obj)
{
var description = document.getElementsById("description");
description.innerHTML="Thank You";
}
function mOut(obj)
{
var description = document.getElementsById("description");
description.innerHTML="Mouse Over Me";
}

Fading in page on load

I am trying to use j-query to fade in my pages body upon load, however for some reason the body's background image is not being affected by the j-query. Please see the code I have below:
J-Query Code in head section:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('body').fadeIn(2000);
});
</script>
CSS Code:
body
{
overflow:hidden;
background:url('body.png') no-repeat;
background-size:100%;
display:none;
}
Everything contained within the body (div's / paragraphs / headings etc) fade in on load as per the j-query code, however the body's background image (body.png) loads instantly with the page. Please can anyone suggest what I'm doing wrong with the above code?
body behaves funny. You would need to wrap the contents of the entire page in another div and fade that in.
<body>
<div id="wrapper">
# Page Contents #
</div>
</body>
CSS:
#wrapper{
background-image:url('some-image.jpg');
display:none;
}
jQuery:
$(document).ready(function(){
$('#wrapper').fadeIn();
});
See this JSFiddle.
Like the #ITFarmer wait, you can do it in CSS3, but you could probably also animate the background-image with CSS3 too.
#keyframes fadein {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
#keyframes fadeinbg {
from {
background-image: none;
}
to {
background:url('body.png') no-repeat;
}
}
.container {
animation: fadein 2s;
}
.body {
animation: fadeinbg 2s;
I'm not sure if it would also affect the background image, but you could do the fade-in effect also without jQuery. At least in browsers which support CSS3:
#keyframes fadein{
from{opacity:0;}
to{opacity:1;}
}
.someElement{
animation: fadein 2s;
}

CSS3 transition with Javascript

i have this Webpage on which with mouse over, the element rotates and goes back to original position on mouseout.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
div
{
width:100px;
height:100px;
background:red;
transition:width 2s;
-moz-transition:width 2s; /* Firefox 4 */
-webkit-transition:width 2s; /* Safari and Chrome */
-o-transition:width 2s; /* Opera */
}
div:hover
{
width:300px;
}
</style>
</head>
<body>
<p><b>Note:</b> This example does not work in Internet Explorer.</p>
<div></div>
<p>Hover over the div element above, to see the transition effect.</p>
</body>
</html>
Now Is it possible to trigger this transition using JavaScript and not the defualt hover function. I tried by creating two different class names but it just dosent work even if i do it by adding delay and changing the class. Is there anyway to do such a transition with Javascript??
I believe all you need to do is fire a change the style of the element you want to modify when particular events happen. Also, I changed the DOCTYPE to use HTML5. Give this a try:
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
div.sample1
{
width:100px;
height:100px;
background:red;
transition:width 2s;
-moz-transition:width 2s; /* Firefox 4 */
-webkit-transition:width 2s; /* Safari and Chrome */
-o-transition:width 2s; /* Opera */
}
div.sample1:hover
{
width:300px;
}
</style>
<script type="text/javascript">
function doStuff(obj,boolStateChange)
{
console.log('test');
if (boolStateChange==true)
{
obj.style.cssText = "width:300px;height:100px;background:green;transition:width 2s;-moz-transition:width 2s;-webkit-transition:width 2s;-o-transition:width 2s;";
}
else
{
obj.style.cssText = "width:100px;height:100px;background:green;transition:width 2s;-moz-transition:width 2s;-webkit-transition:width 2s;-o-transition:width 2s;";
}
}
</script>
</head>
<body>
<p><b>Note:</b> This example does not work in Internet Explorer.</p>
<div class="sample1"></div>
<p>Hover over the div element above, to see the transition effect.</p>
<br/><br/><br/>
<div id="javaHover" style="background-color:green;width:100px;height:100px;" onMouseOver="doStuff(this,true);" onMouseOut="doStuff(this,false);"></div>
<p>Hover over the div element above, to see the Javascript transition effect.</p>
</body>
</html>
You can do this:
document.getElementsByTagName("div")[0].style.width = '300px';
See fiddle.
what do you think about this:
http://jsfiddle.net/scrRe/ ?
Here you go:
http://jsfiddle.net/scrRe/3/
Do not use :hover selector then. Use JavaScript events.
for example:
CSS:
#box
{
background-color: steelblue;
height: 100px;
width: 100px;
transition: height 1s, width 1s;
-moz-transition: height 1s, width 1s;
-webkit-transition: height 1s, width 1s;
-o-transition: height 1s, width 1s;
}
JavaScript:
var element = document . getElementById ( "box" ) ;
element . addEventListener
(
"click",
function ()
{
this . style . width = "200px" ;
}
) ;
element . addEventListener
(
"dblclick",
function ()
{
this . style . height = "200px" ;
}
) ;
http://jsfiddle.net/6gSZD/
You may be able to use CSSAnimation, which is a JavaScript library that allows you to trigger animations with JavaScript while utilizing CSS to do the actual animation.
Here's an example from the documentation (try it):
var element = document.getElementById('some-el'),
transition = new Transition(element),
transform = new Transform(element);
transition.set({
property: 'transform',
'timing-function': 'ease-in',
duration: '2s'
});
transform.rotate(720).scale(2);

Categories

Resources