I am trying to go for a needle effect and I cannot seem to get the needle to rotate from its bottom. I want the bottom of the image to stay fixed in its position and just the top of the needle to move along the half circle. This is was I have so far. Any suggestions on how to get this effect to work?
$(document).ready(function() {
setTimeout(function() {
$(".needle").css("transform", "rotate(0deg)");
}, 1000)
})
figure {
position: relative;
display: inline-block;
}
.graph {
width: 300px;
}
.needle {
position: absolute;
width: 160px;
left: 15px;
bottom: -28px;
transform: rotate(-171deg);
-webkit-transform: rotate(-171deg);
transition: all 7s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<figure>
<img src="http://1.bp.blogspot.com/-XqCc5ql8Xqc/T6DuArRFB0I/AAAAAAAAADA/IK6fKAjzkOc/s1600/Finished+semi-circle+3.png" class="graph" />
<img src="http://www.kiteinnovation.com/wp-content/themes/naked-wordpress-master/images/arrow.png" class="needle" />
</figure>
Use css transform-origin property
$(document).ready(function() {
setTimeout(function() {
$(".needle").css("transform", "rotate(0deg)");
}, 1000)
})
figure {
position: relative;
display: inline-block;
}
.graph {
width: 300px;
}
.needle {
position: absolute;
width: 160px;
right: 15px;
bottom: -28px;
transform: rotate(-171deg);
transform-origin: left;
-webkit-transform: rotate(-171deg);
transition: all 7s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<figure>
<img src="http://1.bp.blogspot.com/-XqCc5ql8Xqc/T6DuArRFB0I/AAAAAAAAADA/IK6fKAjzkOc/s1600/Finished+semi-circle+3.png" class="graph" />
<img src="http://www.kiteinnovation.com/wp-content/themes/naked-wordpress-master/images/arrow.png" class="needle" />
</figure>
Related
I'm trying to make an image semitransparent on hover and display text that is otherwise set to display: none. Currently if you hover over an image, it becomes semi-transparent (.single-image:hover works), but the text doesn't appear. When the text was not set to display: none, it is positioned over the image in the bottom left-hand corner. I thought that since it is over the image, the hover pseudo-class would take effect. I also tried setting the z-index of .attribution to 1 but that didn't do anything.
<div className="image-container">
<a href={image.links.html} target="_blank" rel="noopener noreferrer">
<img className="single-image" src={image.urls.regular} alt="" />
<p className="attribution">Unsplash</p>
</a>
</div>
.image-container {
display: inline-block;
position: relative;
}
/* Text */
.attribution {
display: none;
color: white;
position: absolute;
bottom: 20px;
left: 20px;
}
.attribution:hover {
display: block;
}
.single-image {
margin: 7px;
height: 350px;
width: 350px;
object-fit: cover;
}
.single-image:hover {
opacity: 0.7;
transition: 0.5s;
}
You should move the hover function to the parent div.
Change .attribution:hover to .image-container:hover .attribution and .single-image:hover to .image-container:hover .single-image.
Strangely enough, in order for this to work, you also need to add border or background-color to your parent div. (Here is why)
I added a transparent color background-color: rgba(0, 0, 0, 0);.
.image-container {
display: inline-block;
position: relative;
background-color: rgba(0, 0, 0, 0);
}
/* Text */
.attribution {
display: none;
color: white;
position: absolute;
bottom: 20px;
left: 20px;
}
.image-container:hover .attribution {
display: block;
}
.single-image {
margin: 7px;
height: 350px;
width: 350px;
object-fit: cover;
}
.image-container:hover .single-image {
opacity: 0.7;
transition: 0.5s;
}
<div class="image-container">
<a href="#" target="_blank" rel="noopener noreferrer">
<img class="single-image" src="https://upload.wikimedia.org/wikipedia/commons/2/22/Turkish_Van_Cat.jpg" alt="" />
<p class="attribution">Kitten!</p>
</a>
</div>
.cont {
position: relative;
height: 150px;
width: 150px;
margin: 1em auto;
background-color: red;
}
.layer {
position: relative;
height: 150px;
width: 150px;
color: Transparent;
}
.layer:hover { color: white; }
.cont:hover { background-color: blue;}
<div class="cont">
<div class="layer">My cool Text</div>
</div>
Why did you not set the color of the font to transparent? it's easyer. see my little example (2 minutes workaround...)
Add a bit of javascript to make the text appear aswell as the image fade.
as only one element can be in hover state even if there ontop of each other.
unless you mess around with js you might be able to 'hack' a double hover state.
try;
<div className="image-container">
<a href={image.links.html} target="_blank" rel="noopener noreferrer">
<img className="single-image" src={image.urls.regular} alt="" />
<p className="attribution">Unsplash</p>
</a>
</div>
.image-container {
display: inline-block;
text-align:center;
position: relative;
z-index:1;
}
/* Text */
.attribution {
display: none;
color: white;
position: absolute;
z-index:3;
bottom: 20px;
left: 20px;
}
.single-image {
margin: 7px;
height: 350px;
width: 350px;
object-fit: cover;
z-index:2;
}
.single-image:hover {
opacity: 0.7;
transition:opacity 0.5s ease-in-out;
will-change:opacity;
}
<script>
document.addEventListener(DOMContentLoaded,(e)=>{
const img = document.getElementsByClassName('single-image')[0];
const attrib = document.getElementsByClassName('attribution')[0];
img.addEventListener('mouseover',(e)=>{
img.style.opacity = '0.7';
attrib.style.display = 'block';
});
});
</script>
I have following image as following,
How can i write text on image above and achieve the outcome as the following?
Try this, I hope it'll help you out. Thanks
.wrapper {
position: relative;
}
.wrapper img {
width: 100%;
}
.wrapper label {
color: #fff;
font-size: 24px;
position: absolute;
}
.wrapper #id1 {
left: 20px;
top: 20px;
}
.wrapper #id2 {
right: 20px;
top: 20px;
}
.wrapper #id3 {
left: 20px;
bottom: 20px;
}
.wrapper #id4 {
right: 20px;
bottom: 20px;
}
.wrapper #id5 {
right: 20px;
top: 50%;
transform: rotate(90deg)
}
.wrapper #id6 {
left: 20px;
top: 50%;
transform: rotate(-90deg)
}
<div class="wrapper">
<label id="id1">Text Left!</label>
<label id="id2">Text Right!</label>
<label id="id3">Text Bottom Left!</label>
<label id="id4">Text Bottom Right!</label>
<label id="id5">Text Rotate Right!</label>
<label id="id6">Text Rotate Left!</label>
<img src="https://keyassets.timeincuk.net/inspirewp/live/wp-content/uploads/sites/12/2015/07/Depth-of-field-landscape.jpg" alt="" />
</div>
Assuming you have HTML like:
<div class="container">
<img />
<p>Foo</p>
</div>
something like this would work (you'll have to fiddle the values obviously):
.container {
position: relative;
img{ z-index: 1; // img styles here }
p { position: absolute; left: 0.5rem; top: 0.5rem; z-index: 3; transform: rotate(90deg) }
}
Step by step:
Position the text absolutely
Rotate it 90 degrees
Set a Z-index higher than the image
Position the text correctly on the image
Profit ...
I have this somewhat simple animation. It does the animation just fine when hover over the target elements, but it doesn't transition back to the original position when the cursor hovers outside the target element. Rather, it just snaps back in one fell and ugly swoop. :)
<article class="anim">
<a href="#foo">
<img src="https://dummyimage.com/240x200/ff9900/fff.png&text=TADA" alt="foo">
<span class="left">
<img src="https://dummyimage.com/240x200/121212/fff&text=hey+you" alt="left">
</span>
<span class="right">
<img src="https://dummyimage.com/240x200/000/fff&text=hey+you" alt="right">
</span>
</a>
</article>
And this is the scss:
.anim {
$w: 240px;
$h: 200px;
width: $w;
height: $h;
overflow: hidden;
a {
display: block;
position: relative;
width: 100%;
height: 100%;
}
img {
z-index: 1;
position: absolute;
width: $w;
top: 0;
left: 0;
}
span {
display: block;
height: 100%;
width: 50%;
position: absolute;
z-index: 2;
overflow: hidden;
// This animation snaps back uglily.
transition: all cubic-bezier(.29, 1.01, 1, -0.68) 0.5s;
// This animation transitions back just fine...
//transition: all 0.5s ease-in 0s;
}
.left {
left: 0px;
}
.right {
right: 0px;
img {
margin-left: -100%;
}
}
a:hover > .left {
left: -100%;
}
a:hover > .right {
right: -100%;
}
}
What I don't understand is that the commented out animation (with easy-in) transitions back smootly, but the cubic-bezier one does not.
I don't know what is missing here. Here's a pen:
https://codepen.io/FernandoBasso/pen/XqrOpV?editors=1100
The reason is that in the following code
a:hover > .left {
left: -100%;
}
a:hover > .right {
right: -100%;
}
the values -100% send the divs too far away to see the reverse effect. Using -50% instead does what you want! :)
Check this out:
https://codepen.io/sotmihos/pen/yjLqJK
you can reverse the cubic-bezier to achieve that :
( don't mind the javascript function it's there just to show how to reverse the cubic-bezier )
function reverseCssCubicBezier(cubicBezier) {
var maxX = Math.max(cubicBezier[0].x, cubicBezier[1].x, cubicBezier[2].x, cubicBezier[3].x);
var maxY = Math.max(cubicBezier[0].y, cubicBezier[1].y, cubicBezier[2].y, cubicBezier[3].y);
var halfUnitTurn = function(v) {
var tx = maxX/2, ty = maxY/2;
return { x: tx - (v.x-tx), y: ty - (v.y-ty) };
};
var revd = cubicBezier.map(halfUnitTurn);
return revd.reverse();
}
var ease = [{x:0,y:0}, {x:0.29,y:1.01}, {x:1,y:-0.68}, {x:1,y:1}];
var ease_reversed = reverseCssCubicBezier(ease);
var ease_css_string = 'cubic_bezier(' + [ease[1].x, ease[1].y, ease[2].x, ease[2].y].join(', ') + ')';
var ease_reversed_css_string = 'cubic_bezier(' + [ease_reversed[1].x, ease_reversed[1].y, ease_reversed[2].x, ease_reversed[2].y].join(', ') + ')';
console.log('ease_css_string : ', ease_css_string)
console.log('ease_reversed_css_string : ', ease_reversed_css_string)
.anim {
width: 240px;
height: 200px;
overflow: hidden;
}
.anim a {
display: block;
position: relative;
width: 100%;
height: 100%;
}
.anim img {
z-index: 1;
position: absolute;
width: 240px;
top: 0;
left: 0;
}
.anim span {
display: block;
height: 100%;
width: 50%;
position: absolute;
z-index: 2;
overflow: hidden;
transition: all cubic-bezier(0, 1.69, 0.71, 0) 0.5s; /* added */
}
.anim .left {
left: 0px;
}
.anim .right {
right: 0px;
}
.anim .right img {
margin-left: -100%;
}
.anim a:hover > span {
transition: all cubic-bezier(0.29, 1.01, 1, -0.68) 0.5s; /* added */
}
.anim a:hover > .left {
left: -100%;
}
.anim a:hover > .right {
right: -100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<article class="anim">
<a href="#foo">
<img src="https://dummyimage.com/240x200/ff9900/fff.png&text=TADA" alt="foo">
<span class="left">
<img src="https://dummyimage.com/240x200/121212/fff&text=hey+you" alt="left">
</span>
<span class="right">
<img src="https://dummyimage.com/240x200/000/fff&text=hey+you" alt="right">
</span>
</a>
</article>
According to this link Repost or flag my own question for migration?
I'have to repost my question for a useful answer.
On hover, the doors are supposed to be open, according to the CSS. It works in Firefox, Opera, Chrome and IE but there is no effect on hover in Safari 5.1.7. Where's the problem here? The part of the CSS for this hover is the following:
$(document).ready(function() {
$("#cupboard").on("touchstart", function(e) {
$(this).addClass("hover");
e.preventDefault();
});
$("body").on("touchend", function(e) {
$("#cupboard").removeClass("hover");
});
});
html,
body {
margin: 0;
padding: 0;
background: #C2B3A0;
}
/*user-select: none;*/
/*background: url(bg.jpg);*/
#cupboard {
height: 613px;
width: 617px;
position: relative;
left: 35%;
margin-left: -112px;
top: 24px;
bottom: 31px;
perspective: 500;
background: url(bg.png);
background-position: center center;
background-size: 95%;
background-repeat: no-repeat;
background-attachment: fixed;
}
#cupboard img {
position: inherit;
height: 200px;
width: 200px;
float: left;
margin-left: 62px;
margin-top: 82px;
}
#cupboard .door#left {
zoom: .6;
position: absolute;
width: 512px;
height: 100%;
background-image: url(dl.jpg);
transition: transform 1s ease;
transform: rotateY(0);
}
#cupboard .door#right {
zoom: .6;
position: absolute;
width: 517px;
height: 100%;
background-image: url(dr.jpg);
transition: transform 1s ease;
transform: rotateY(0);
}
#cupboard .door#left {
transform-origin: top left;
left: 0;
}
#cupboard .door#right {
transform-origin: top right;
right: 0;
}
#cupboard:hover #left,
#cupboard.hover #left {
-webkit-transform: rotateY(-90deg);
transform: rotateY(-90deg);
}
#cupboard:hover #right,
#cupboard.hover #right {
-webkit-transform: rotateY(90deg);
transform: rotateY(90deg);
}
<html>
<head>
<script src="http://s.codepen.io/assets/libs/prefixfree.min.js"></script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="script.js"></script>
<link rel="stylesheet" type="text/css" href="style1.css">
</head>
<body>
<div id="cupboard">
<a href="http://www.facebook.com">
<img src="f.jpeg" alt="facebook" />
</a>
<a href="http://www.twitter.com">
<img src="t.jpeg" alt="twiter" />
</a>
<a href="http://www.linkedin.com">
<img src="l.png" alt="linkedin" />
</a>
<a href="http://www.wordpress.com">
<img src="w.jpg" alt="wordpress" />
</a>
<div class="door" id="left"></div>
<div class="door" id="right"></div>
</div>
</body>
</html>
Here is the sreenshot of output page:-
Before hover :-
After hover :-
Screenshot of Safari
According to caniuse, safari needs the -webkit- prefix to work. Also you don't have any units after your perspective length.
Edit from your comment for completeness:
Also need to use -webkit-transition: all 1ms ease;
Css transition codes for each of browsers is different and you should set them for all browsers.
Please visit to css tricks site or w3school site for standard sintax of it for safari browser.
I am using this fork code to display text on mouseover of an image.
http://codepen.io/anon/pen/hxqoe
HTML
<img src="http://placekitten.com/150" alt="some text" />
<img src="http://placekitten.com/150" alt="more text" />
<img src="http://placekitten.com/150" alt="third text" />
<div id="text"></div>
CSS
div {
display: none;
position: absolute;
}
img:hover + div {
display: block;
}
JQUERY
$('img').hover(function() {
$('div').html($(this).attr('alt')).fadeIn(200);
}, function() {
$('div').html('');
});
I am looking for the div text to be displayed inside each image instead of a stationary div.
Any ideas?
You actually don't need jQuery for this. It can easily be achieved with pure CSS.
In this example, I use the :after pseudo element to add the content from the alt attribute of the parent div. You can add whatever styling you want..
jsFiddle here - Updated to include a fade
HTML - pretty simple
<div alt="...">
<img src="..."/>
</div>
CSS
div {
position: relative;
display: inline-block;
}
div:after {
content: attr(alt);
height: 100%;
background: rgba(0, 0, 0, .5);
border: 10px solid red;
position: absolute;
top: 0px;
left: 0px;
display: inline-block;
box-sizing: border-box;
text-align: center;
color: white;
padding: 12px;
font-size: 20px;
opacity: 0;
transition: 1s all;
-webkit-transition: 1s all;
-moz-transition: 1s all;
}
div:hover:after {
opacity: 1;
}
Change your javascript to this:
$('img').hover(function() {
$('div').html($(this).attr('alt')).fadeIn(200);
$('div').css('left',$(this).offset().left + 'px');
$('div').css('top',$(this).offset().top + $(this).height() - 20 + 'px');
}, function() {
$('div').html('');
});
And that should do the trick.
I would wrap your images in some kind of wrapper and then use CSS for the hover effect (DEMO).
HTML:
<div class="img-wrapper">
<img src="http://placekitten.com/150" alt="some text 1" />
<div class="img-desc">some text 1</div>
</div>
CSS:
.img-wrapper {
position: relative;
display: inline;
}
.img-desc {
display: none;
position: absolute;
bottom: 0px;
left: 0px;
width: 100%;
background: #FFF;
opacity: 0.6;
padding: 5px;
}
.img-wrapper:hover .img-desc{
display: block;
}
I added an alternative solution with additional css transition to the demo.
To get a transition just control the visibility with opacity:
.img-desc {
/*...*/
opacity: 0;
/*...*/
}
.img-wrapper:hover .img-desc{
opacity: 0.6;
transition: opacity 0.4s ease-in;
}