Make text appear somewhere else on hover - html

I have an image, and when I hover over the image, I want to make text appear somewhere else on the screen. I've found lots of solutions making text appear on the image, but not somewhere else on the page. Any ideas?
Here's what I'm trying:
HTML:
<div class="dan-profile-picture"></div>
blah blah blah
<div class="dan-profile">lots of text</div>
CSS:
div.dan-profile-picture {
width: 150px;
height: 150px;
border-radius: 75px;
-webkit-border-radius: 75px;
-moz-border-radius: 75px;
background: url(./images/dan.jpg) no-repeat;
border:3px solid black;
}
div.dan-profile {
display:none;
}
div.dan-profile-picture:hover {
opacity:0.5;
}
div.dan-profile-picture:hover + div.dan-profile {
display:block;
}

if you prefer jQuery :
$('.dan-profile-picture').hover(function () {
var posx = Math.floor(Math.random() * $("body").width());
var posy = Math.floor(Math.random() * $("body").height());
$('.dan-profile').css({
'position': 'absolute',
'left': posx + 'px',
'top': posy + 'px',
'display': 'block'
});
});
Fiddle Demo
update
to hide the div, you can use either a call back function in existing jQuery - as mentioned in comments by user3008011
$('.dan-profile-picture').hover(function () {
var posx = Math.floor(Math.random() * $("body").width());
var posy = Math.floor(Math.random() * $("body").height());
$('.dan-profile').css({
'position': 'absolute',
'left': posx + 'px',
'top': posy + 'px',
'display': 'block'
});
},function(){ //this is call back function
console.log('out'); //will be triggered when main
$('.dan-profile').hide(); //function completes
});
demo
second way - since you are new to jQuery - you can trigger hide even on mouseout
$('.dan-profile-picture').mouseout(function () {
$('.dan-profile').css({
'display': 'none'
});
});
demo

make dan-profile a child of dan-profile-picture
.dan-profile-picture{
position:relative;
}
.dan-profile{
position:absolute;
right: -30px /* whichever value you desire */
top: -30px /*whichever value you desire */
display:none;
}
.dan-profile-picture:hover .dan-profile{
display:block;
}
hope this helps

This should do the trick.
div.dan-profile-picture {
width: 150px;
height: 150px;
border-radius: 75px;
-webkit-border-radius: 75px;
-moz-border-radius: 75px;
background: url(./images/dan.jpg) no-repeat;
border:3px solid black;
}
.dan-profile-picture:hover {
opacity:0.5;
}
.dan-profile{display:none;}
.dan-profile-picture:hover + .dan-profile{display:block;}
Fiddle
Remember that with the + selector, the element must be the directly next element. If there are elements in between, this selector won't work and you'll have to resort to javascript.
See this page for more info on css selectors
Number 7. of that list is the one I used for your user case
"This is referred to as an adjacent selector. It will select only the element that is immediately preceded by the former element."

Related

Can animation only be applied to 'absolute' positioned elements?

I am trying to animate an object using DOM and struggling to animate the element when its CSS property position is not set to "absolute". Here is my code below:
I create a circle HTML element and try to move it in 45 degrees. Is there any way to animate an HTML element object that is not positioned absolute?
x = 10;
function on_click() {
var myCurvyMovement = document.getElementById("circle");
myCurvyMovement.style.left = 0.5 * x;
myCurvyMovement.style.top = 1 + x
x += 10;
}
#circle {
position: absolute;
width: 100px;
height: 100px;
background: red;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
}
/* Cleaner, but slightly less support: use "50%" as value */
#divBox {
position: static
}
<body>
<button style="display:block" onclick="on_click()">Move the box</button>
<div id="circle">
</div>
</body>
I wouldn't consider left/right in order to do animation. As you have noticed, it won't work in all the cases as it need positionned elements. Even when using positionned element you won't have the same behavior between relative, absolute and fixed because each one will have its own reference for top/left.
For such case better consider transform that you can apply to any element (shouldn't be an inline element) and the reference of the movement will be the same for all. You will also have better performance.
x = 10;
function on_click() {
var myCurvyMovement = document.getElementById("circle");
myCurvyMovement.style.transform = "translate(" + (0.5 * x)+"px,"+(1 + x)+"px)";
x += 10;
}
#circle {
width: 100px;
height: 100px;
background: red;
border-radius: 50px;
transition:0.5s all; /*to have a smooth movement*/
}
<body>
<button style="display:block" onclick="on_click()">Move the box</button>
<div id="circle">
</div>
</body>
You forgot to concatenate the "px" to set the x and y positions
x = 10;
function on_click() {
var myCurvyMovement = document.getElementById("circle");
myCurvyMovement.style.left = 0.5 * x + 'px';
myCurvyMovement.style.top = 1 + x + 'px';
x += 10;
}
#circle {
position: absolute;
width: 100px;
height: 100px;
background: red;
-moz-border-radius: 50px;
-webkit-border-radius: 50px;
border-radius: 50px;
}
/* Cleaner, but slightly less support: use "50%" as value */
#divBox {
position: static
}
<body>
<button style="display:block" onclick="on_click()">Move the box</button>
<div id="circle">
</div>
</body>
when not's absolute you need change the margin-left and margin-top property, in javascript is like this
myCurvyMovement.style.marginLeft = 1 + x + 'px'
myCurvyMovement.style.marginTop = 1 + x + 'px'
(top/bottom and left/rigth)

How to rotate the block every time you click on the button?

More precisely, the rotation of the block does not work.
At the moment, the block turns once, and I want to turn every time i click in both directions.
how to do it ?
var now = 10
$('.left').click(function() {
$(".d").css('transform', 'rotate (' + now + 'deg)');
});
.r {
width: 200px;
height: 200px;
border: 20px solid;
border-radius: 50%;
}
.d {
width: 200px;
height: 200px;
border: 20px solid blue;
border-radius: 50%;
}
.wrapper {
width: 230px;
}
.parent {
width: 240px;
height: 240px;
position: relative;
}
.r,
.d {
position: absolute;
}
.d {
border-right-color: transparent;
border-top-color: transparent;
transform: rotate(45deg);
}
.btns {
display: table;
margin: 10px auto;
}
button {
margin-left: 10px;
}
<div class="wrapper">
<div class="parent">
<div class="r"></div>
<div class="d"></div>
</div>
<div class="btns">
<button class="left">+</button>
<button class="right">-</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
At the very beginning I wanted to make such a regulator as in the screenshot.
but since there is no great knowledge in JS and I decided to do it in HTML and CSS + jQuery
wanted to use svg but I don't know how to change it
hope for your help
You need to keep a running total in now so that you can keep incrementing. For example:
var now = 0;
$('.left').click(function() {
now += 10;
$(".d").css('transform', 'rotate(' + now + 'deg)');
});
$('.right').click(function() {
now -= 10;
$(".d").css('transform', 'rotate(' + now + 'deg)');
});
As another person has just pointed out, your spaces between rotate and ( were also breaking it.
Here's a CodePen if you need it: https://codepen.io/MSCAU/pen/MZgqop
I think the Problem is with unnecessaryspaces that I remove I hope to help you.
var now = 10;
$('.left').click(function() {
now += 10;
$(".d").css('transform', 'rotate('+now+'deg)');
});
$('.right').click(function() {
now -= 10;
$(".d").css('transform', 'rotate('+now+'deg)');
});
In this case you have to increase or decrease 10 deg again and again here is an example:
$(function(){
var now = 10,
count = 0;
$('.left').click(function() {
rotate( count + 1);
});
$('.right').click(function() {
rotate( count - 1);
});
function rotate(new_count) {
var rotatePx = ((new_count) * (now));
$(".d").css('transform', 'rotate('+ rotatePx +'deg)');
count = new_count;
}
});

How to move textfield up when keyboard appears

In my website textfield not focusing up when default android keyboard comes. It hides the textbox. any simple solution for that issue.
there will be position:fixed property (in a div related to the text field) ,which prevents the text field from jumping up ...just disable the position property ... i had the same problem
this is a basic code to scroll page up and down when virtual keyboard appears
JsFiddle Demo
jQuery
var keyboardHeight = 150;
$(document).ready(function() {
$("textarea, input").focusin(function() {
var element = $(this);
var viewportHeight = $(window).height();
var windowScrollTop = $(window).scrollTop();
var offsetTop = element.offset().top - windowScrollTop;
var validOffset = viewportHeight - keyboardHeight;
$("#keyboard").fadeIn();
if(offsetTop <= 0) {
$('html, body').animate({
scrollTop: windowScrollTop + offsetTop
}, 1000);
}
else {
if(viewportHeight > keyboardHeight) {
if(offsetTop > validOffset) {
$('html, body').animate({
scrollTop: windowScrollTop + offsetTop
}, 1000);
}
}
}
});
$("textarea, input").focusout(function() {
$("#keyboard").fadeOut();
});
});
html
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<textarea rows="11"></textarea>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="keyboard"></div>
css
body {
background: transparent;
padding-bottom: 150px; /* height of virtual keyboard */
}
textarea {
width: 100%;
background: transparent;
border: solid 1px red;
border-radius: 10px;
color: white;
outline: none;
}
#keyboard {
position: fixed;
width: 100%;
height: 150px;
left: 0;
bottom: 0;
background: rgba(13, 13, 13, 0.5);
display: none;
}

Is there a way to change the style of a div's :after psuedoelement when hovering over it's :before psuedoelement?

Given the following css:
.myDiv:before{
content:'';
width:15px;
height:15px;
display:block;
}
.myDiv:after{
...
...
display:none;
}
and html:
<div class='myDiv'></div>
Is there a way to show the .myDiv:after psuedoelement while hovering over the :before? I know I can use the hover selector as .myDiv:hover:before but I don't know how to access the :after psuedoelement from within that selector.
You can add a new style to display the content in the :after css class although it may not be the best practice.
.myDiv:hover:after {
display:block;
}
http://jsfiddle.net/gk7R2/1/
The only close way that you can do it is if you hover the entire element like this:
.myDiv {
height: 200px;
background: red;
}
.myDiv:before {
content: '';
display: block;
height: 20px;
width: 100%;
background: blue;
}
.myDiv:hover:after {
content: '';
display: block;
height: 20px;
width: 100%;
background: blue;
}
http://jsfiddle.net/Hive7/W4ca3/
You are not able to trigger the :hover::after from executing a :hover::before. You can solve this problem with javascript though. If you're wanting to maintain a clean markup, you could create a loop within this fiddle and dynamically add placeholders behind the pseudo elements so that you can interact with them instead of trying to access pseudo elements that do not exist in the DOM.
$("head").append("<style id='pseudoElementStyle'></style>");
var setPseudoElementDisplay = function (hover) {
var content = hover ? ' after' : '';
var cssRule = '.text::after { content: "' + content + '"; }';
$('#pseudoElementStyle').text(cssRule);
};
$('.text').prepend('<span class="place-holder before">before </span>');
$('.text').append('<span class="place-holder after"> after</span>');
$('.place-holder').each(function () {
var direction;
var width = $(this).css('width');
direction = $(this).hasClass('before') ? 'left' : 'right';
$(this).css('margin-' + direction, '-' + width);
});
$('.before').mouseover(function () {
setPseudoElementDisplay(true);
}).mouseout(function () {
setPseudoElementDisplay(false);
});
http://jsfiddle.net/jasonjohnson115/2d96N/

Scale div to fit page

I'm very new to CSS and HTML and I'm trying to create a CSS version of this:
http://i.stack.imgur.com/JNgUb.jpg
I've successfully created 4 divs with 4 different colors but this is my result:
http://i.imgur.com/ihYblSv.png
How can I scale the div to fit the entire page?
my code is:
body{
background-color: #eae1c8;
}
#bg {
transform:rotate(30deg);
-webkit-transform:rotate(30deg);
-moz-transform:rotate(30deg);
-ms-transform:rotate(30deg);
-o-transfrom:rotate(30deg);
}
#blue {
height: 25%;
background-color: #9dd2b5;
}
#green {
height: 25%;
background-color: #6aa427;
}
#yellow {
height: 25%;
background-color: #f0b747;
}
#orange {
height: 25%;
background-color: #de5b1e;
}
It fit the entire page before you rotated it.
If you want it to take up the entire page, set page body { overflow: hidden; } and then play with the sizes of your divs to fill the space. Set #bg { height: 160%; } and each color to 40% and see how that works.
Here is a Javascript Function I created with a little CSS. This is great for auto-sizing a page to fit without changing the ratio. This will resize the page on startup and when the window is resized. You can change body to the element that you want full page, and you can change the function to use a click event if you want it resized only when it is clicked. Just remove the call for the function and change the $(window).resize to $('#elementid').click
CSS:
body{
height:620px;
width:1023px;
position:absolute;
box-sizing:border-box;
transform-origin: 0 0;
-moz-transform-origin:0 0;
-o-transform-origin: 0 0;
-webkit-transform-origin: 0 0;
}
JavaScript:
var ratio;
var left;
resize();
$(window).resize(function () {resize();});
function resize()
{
ratio = window.innerHeight / $('body').innerHeight();
if (window.innerWidth / $('body').innerWidth() < ratio) {
ratio = window.innerWidth / $('body').innerWidth();
}
ratio -= .04;
$('body').css('-ms-zoom', ratio);
$('body').css('-moz-transform', 'scale(' + ratio + ')');
$('body').css('-o-transform', 'scale(' + ratio + ')');
$('body').css('-webkit-transform', 'scale(' + ratio + ')');
$('body').css('transform', 'scale(' + ratio + ')');
left = ($(window).innerWidth() - $('body').outerWidth() * ratio) / 2;
$('body').css('left', left);
}