Image overlay is flickering? - javascript

**** The truth is all these solutions work, just not with the project so I will re-pose the question, slightly differently ****
Essentially I have an image, which when someone moves their mouse cursor over it, it displays a div (which contains an image - aka play button). When they move they're cursor outside of the image, the play button disappears.
It works but if you move the curse over the play button it flickrs like mad, hence I must have my events mixed up somehow..
Thanks in advance for any help...
HTML
<div id="cont">
<div id="art" style= "position: absolute; left: 20px; top: 40px;">
<img src="http://www.newmusicproducer.com/img/ic_play.png">
</div>
<img src="https://i1.sndcdn.com/avatars-000008744854-5sr588-large.jpg?d408275" alt="smile" id="imgSmile"></img>
jQuery
$(document).ready(function() {
$('#art').hide();
$('#imgSmile').mouseenter(function() {
$('#art').show();
});
$('#imgSmile').mouseleave(function() {
$('#art').hide();
});
});​
Here's a fiddle

This same effect could be achieved using just CSS:
<div>
<img src="http://www.newmusicproducer.com/img/ic_play.png">
</div>
div
{
background-image:url('https://i1.sndcdn.com/avatars-000008744854-5sr588-large.jpg?d408275');
width:100px;
height:100px;
position:relative;
}
div a
{
position:absolute;
top:20px;
left:30px;
display:none;
}
div:hover a
{
display:block;
}
​-- SEE DEMO --
EDIT: In case you need to use this multiple times on the same page with different link URLs, I've made a version which requires minimal HTML using a bit of jQuery to apply the rest:
<div class="play kirkbridge" data-playUrl="/myPlayUrl"></div>
http://jsfiddle.net/tW68d/16/
This might be a bit overkill though, it depends on your usage.

The problem is mousing over the new image is causing mouseout on the original. Try using hover() instead, along with both elements as the selector.
$(document).ready(function() {
$('#art').hide();
$('#imgSmile, #art').hover(
function() {
$('#art').show();
}, function() {
$('#art').hide();
}
);
});
Updated fiddle

Here's an alternative approach:
http://jsfiddle.net/aramk/ZqVZ6/1/
$(document).ready(function(){
var art = $('#art');
art.hide();
var updatePlayButton = function() {
if (art.is(':visible')) {
art.hide();
} else {
art.show();
}
}
$('#cont').hover(updatePlayButton);
});​
I'd use a wrapping DIV, since when the button overlaps the smile image and your mouse is over it you lose focus from the smile image and it starts flashing. This will avoid that.

can be done using CSS too, if thats a posibility.
here is the fiddle http://jsfiddle.net/xasy4/
<div id="imgSmile">
<div id="art">
<a href="#"><img src="http://www.newmusicproducer.com/img/ic_play.png">
</a>
</div>
</div>
the css
#imgSmile
{
background: url('https://i1.sndcdn.com/avatars-000008744854-5sr588-large.jpg?d408275') no-repeat center Transparent;
width: 100px;
height: 100px;
}
#imgSmile > #art
{
position: absolute; left: 20px; top: 40px; opacity: 0;
}
#imgSmile:hover > #art
{
position: absolute; left: 20px; top: 40px; opacity: 1;
}

You can use the .on() method, than set both element as selectors and use an event (e) checker like:
demo jsBin
$(document).ready(function(){
$('#art').hide();
$('#imgSmile, #art').on('mouseenter mouseleave',function(e){
if(e.type === 'mouseenter'){
$('#art').show();
}else{
$('#art').hide();
}
});
});
What I like the most is the use of the Ternary operator like:
$('#imgSmile, #art').on('mouseenter mouseleave',function(e){
var et = e.type === 'mouseenter' ? $('#art').show() : $('#art').hide();
});
Demo with ternaly operator
You can use in your exact case just the .toggle() method:
$('#imgSmile, #art').on('mouseenter mouseleave',function(e){
$('#art').toggle();
});
demo with .toggle()

http://jsfiddle.net/jqkBx/1/
Why dont you just use CSS for hover effects?
HTML
<div id="wrapper">
<img src="https://i1.sndcdn.com/avatars-000008744854-5sr588-large.jpg?d408275" alt="smile" id="imgSmile" />
<div id="art" style="position: absolute; left: 20px; top: 40px;">
<img src="http://www.newmusicproducer.com/img/ic_play.png" />
</div>
</div>
CSS
#art{
display:none;
}
#wrapper{
width:100px;
height:100px;
}
#wrapper:hover #art{
display:block;
}
​

$(document).ready(function() {
$('#art').hide();
$('#imgSmile').mouseenter(function() {
$('#art').show();
});
$('#art').mouseenter(function() {
$('#art').show();
});
$('#imgSmile').mouseleave(function() {
$('#art').hide();
});
});​
just add #art mouse enter event

Related

How can I fade in text with toggle when clicking on an image?

I would like to fade in and out text when clicking on an image. I'm pretty new to JS, so thank you for answering my question!
Here's my jsfiddle: http://jsfiddle.net/gSVfV/
Here's what I have for JS:
$(function() {
$('.block img.info').click(function(e) {
e.preventDefault();
$('.caption-background').toggleClass('hidden');
});
});
For HTML:
<div class="block">
<img class="info"/>
<div class="caption-background">
Hello
</div>
And for css:
.info {
width: 200px;
height: 100px;
background-image:
url("http://ocean.nationalgeographic.com/u/TvyamNb-
BivtNwpvn7Sct0VFDulyAfA9wBcU0gVHVnqC5ghqXjggeitqtJ-
1ZIZ1rmCgor42TXOteIQU/");
}
.caption-background {
height: 200px;
}
.hidden {
display: none;
}
You can use the jQuery fadeToggle method.
Update your javascript with the following:
$(function() {
$('.rsABlock img.info').click(function(e) {
e.preventDefault();
$('.caption-background').fadeToggle();
});
});
Check out http://api.jquery.com/fadetoggle/

How to make toggle off when on mouse over is done?

I have created an element that is displayed when I am over a particular box.
If I move my mouse over the box I can see my element but then I need to move my mouse in and out twice for the element to disappear. How can I fix it? Shouldn't the element hide itself once I move the mouse out?
How do I make my box only show when mouse is over the box?
<script>
$("#box").on("mouseover",function()
{
$("#my-box").toggle();
});
</script>
I tried to hide it myself, but it didn't work:
$("#box").on("onmouseout", function()
{
$("#my-box").hide();
});
You can use mouseover and mouseout in a same eventlistener like one below:
$("#box").on("mouseover mouseout",function()
{
$("#my-box").toggle();
});
#my-box{
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="box">
Box here
</div>
<div id="my-box">
My Box
</div>
FIDDLE DEMO
The problem with your code is you're using onmouseout instead of use mouseenter and mouseleave method.
You can use hover:
$('#box').hover(function(){
$('#my-box').toggle();
});
You can use combination of both
$("#box").mouseover(function() {
$("#my-box").show();
}).mouseout(function() {
$("#my-box").hide();
});
Example
jQuery Solution
HTML
<div class="box" id="action"></div>
<div class="box" id="hidden"></div>
JS
$("#action").on("mouseover mouseout",function()
{
$("#hidden").toggle();
});
CSS
.box{
width: 30px;
height: 30px;
background: red;
margin: 10px;
}
#hidden{
display: none;
}
JSFiddle
Allthough it would be better doing this by just using CSS.
CSS Only Solution
.box{
width: 30px;
height: 30px;
background: red;
margin: 10px;
}
#action:hover + #hidden{
display: block;
}
#hidden{
display: none;
}
JSFiddle

Revert CSS transition on click

I've got a div learn-more that expands to the size of the whole page by adding a CSS class as follows:
.learn-more{
padding:10px;
font-size: 20px;
text-align: center;
margin-top:20px;
font-family: klight;
-webkit-transition:2s;
-moz-transition:2s;
transition:2s;
margin-left: auto;
margin-right: auto;
cursor: pointer;
}
.clicked{
width:100% !important;
visibility: visible;
height: 600px !important;
margin-top:-111px !important;
z-index: 10;
}
This addition is done using JS as follows:
$('.learn-more').on('click', function(){
$(this).addClass('clicked');
$(this).addClass('fire-inside');
});
Now the problem is that I have a button inside the expanded div to reduce the size of this same expanded window.
The JavaScript to do the same is
$('.close-overlay').on('click', function(){
$('.learn-more-btn').removeClass('clicked');
$('.learn-more-btn').removeClass('fire-inside');
});
However this doesn't work as the browser is reading the click on close-overlay as a click on learn-more as the HTML for it as
<div class="learn-more fire-btn">
<p class="fmore">
Find out more
</p>
<div class="inside-text">
<p >
...
</p>
<p class="close-overlay">Close</p>
</div>
</div>
I've added a fiddle for it:
DEMO: http://jsfiddle.net/Newtt/AqV96/
On clicking close, I need the overlay to revert to original size.
Try this on for size!
$(".fmore").on("click", function () {
$(".learn-more").addClass("clicked");
});
$(".close-overlay").on("click", function () {
$(".learn-more").removeClass("clicked");
});
$('.learn-more').on('click', function(){
if ( $(this).hasClass('clicked') )
$(this).removeClass('clicked').removeClass('fire-inside');
else
$(this).addClass('clicked').addClass('fire-inside');
});
Just use the same on click event and check if it has class 'clicked'. You could also use toggle function.

Jquery motion blur

I am working for a company and converting flash ad banners in html5.
I need to convert flash image which slides in from the left and at the same time it performs motion blur effect just like a windy effect.
I have converted slide in image but I am not sure how to add a windy effect.
Here is the car image I want to copy and this is my code jsfiddle
Thanks in advance.
HTML
<div id = "wrapper" >
<div id="mainContainer">
<div id="text">
<img id="Image_Car" src="http://i.share.pho.to/c43dc6d7_o.png" />
</div>
</div>
</div>
CSS
#wrapper {
left: 50px;
top: 50px;
width: 300px;
height:250px;
position: absolute;
}
#mainContainer {
background: url('https://secure-ds.serving-sys.com/BurstingRes/Site-8188/Type-0/5fefb401-b187-4d82-b4db-cbd2ef29cc48.gif');
width:300px;
height:250px;
overflow: hidden;
position: absolute;
}
#Image_Car {
position:absolute;
overflow: hidden;
margin:60px 8px;
left: -120px;
}
jQuery
$(document).ready(function () {
bannerAnimation();
});
function bannerAnimation() {
//Jquery Animation
$("#Image_Car").animate({
left: "30"
}, 500, function () {
$("#Image_Car").animate({
left: "10"
}, 200);
});
}
Jquery is not going to help you create motion blur unless you use a canvas, which nearly no one use for creating ads,
or,
you can create multiple instance of the images and place in same place, then animate each with slight time interval and opacity.
This is very good plugin for you:
DEMO
plugin

How to fade in a div on hover?

I want to hover on an image, and have a div with text in it to fade in. Simple.
Sidenote: I want it to be style-able by CSS. (Kind of a given.) Also, I tried using some examples but I am in a hurry so please and thankyou.
If your using jQuery, Something like:
$('element').hover(function()
{
$('div').animate({ 'opacity': 1 });
});
Try this:
<div class="hover">
<img src="http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" alt="google" />
</div>
<div class="fade" style="display:none">
This text will Fade In and Out.
</div>​
$(document).ready(function()
{
$("div.hover").hover(
function () {
$("div.fade").fadeIn('slow');
},
function () {
$("div.fade").fadeOut('slow');
}
);
});​
Sample code here
By the way, how do you want it to be CSS stylable?
Try this:
http://jsfiddle.net/2WYtS/
You can also use the fadeIn() and fadeOut() methods.
Link
Using CSS You can change fade out using opacity
try This
.box{
width: 180px;
text-align: center;
background-color: #fff;
padding: 5px 10px;
z-index: 10;
font-weight: bold;
color:#000;
}
.box:hover{
opacity: 0.7;
}

Categories

Resources