Make CSS tooltip follow cursor - javascript

I'm creating a CSS based tooltip that is going to have a lot of content in the tooltip and instead of being in a static place I was wondering is there a easy way to make it follow the cursor as you hover over the link.
Here is a example of the CSS based tooltip
<div class="couponcode">First Link
<span class="coupontooltip">Content 1</span>
</div>
.couponcode:hover .coupontooltip {
display: block;
}
.coupontooltip {
display: none;
background: #C8C8C8;
margin-left: 28px;
padding: 10px;
position: absolute;
z-index: 1000;
width:200px;
height:100px;
}
http://jsfiddle.net/q46Xz/

Something like this
var tooltip = document.querySelectorAll('.coupontooltip');
document.addEventListener('mousemove', fn, false);
function fn(e) {
for (var i=tooltip.length; i--;) {
tooltip[i].style.left = e.pageX + 'px';
tooltip[i].style.top = e.pageY + 'px';
}
}
FIDDLE

Here's a version that keeps into account the height and width of your window:
function showTooltip(e) {
var tooltip = e.target.classList.contains("coupontooltip")
? e.target
: e.target.querySelector(":scope .coupontooltip");
tooltip.style.left =
(e.pageX + tooltip.clientWidth + 10 < document.body.clientWidth)
? (e.pageX + 10 + "px")
: (document.body.clientWidth + 5 - tooltip.clientWidth + "px");
tooltip.style.top =
(e.pageY + tooltip.clientHeight + 10 < document.body.clientHeight)
? (e.pageY + 10 + "px")
: (document.body.clientHeight + 5 - tooltip.clientHeight + "px");
}
var tooltips = document.querySelectorAll('.couponcode');
for(var i = 0; i < tooltips.length; i++) {
tooltips[i].addEventListener('mousemove', showTooltip);
}
.couponcode {
color: red;
cursor: pointer;
}
.couponcode:hover .coupontooltip {
display: block;
}
.coupontooltip {
position: absolute;
white-space: nowrap;
display: none;
background: #ffffcc;
border: 1px solid black;
color: black;
padding: 5px;
z-index: 1000;
}
Lorem ipsum dolor sit amet, <span class="couponcode">consectetur
adipiscing<span class="coupontooltip">This is a tooltip</span></span>
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in <span
class="couponcode">reprehenderit<span class="coupontooltip">This is
another tooltip</span></span> in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est <span
class="couponcode">laborum<span class="coupontooltip">This is yet
another tooltip</span></span>.
(see also this Fiddle)

Related

how to create a scrollable horizontal navigation menu but display when the user window scroll down

I want to create a navigation menu that must be scroll horizontally. But this navigation should be display when the user scroll down the window in the browser (Mobile+Desktop).
I have tried out this code so that the navigation menu display after scroll bottom but its item does not scroll horizontally. When removing position: fixed then it's item scroll horizontally.
So I want both functionalities scrollable navigation horizontal+display navigation menu bar when the user scrolls down the window.
window.onscroll = function() {
scrollFunction()
};
function scrollFunction() {
if (document.body.scrollTop > 150 || document.documentElement.scrollTop > 150) {
document.getElementById("navigation").style.top = "0";
} else {
document.getElementById("navigation").style.top = "-150px";
}
}
#navigation {
top: -50px;
position: fixed;
}
.nav-header {
background: #002347;
color: #ffffff;
}
.nav-header a {
color: #ffffff;
}
.nav-header nav {
width: 100%;
}
.itemlist {
padding: 6px 12px;
}
.itemlist:not(:last-child) {
border-right: 1px solid #ece4e4;
}
.itemlist {
font-size: 17px;
font-weight: bold;
}
.itemlist,
.center {
cursor: pointer;
vertical-align: middle;
display: inline-block;
}
.scrolltab {
overflow-x: auto;
white-space: nowrap;
-webkit-overflow-scrolling: touch;
-ms-overflow-style: -ms-autohiding-scrollbar;
}
.scrolltab::-webkit-scrollbar {
display: none;
}
<div id="navigation" class="nav-header">
<nav class="center scrolltab">
<span class="itemlist"> About us </span>
<span class="itemlist">Web Solution</span>
<span class="itemlist">Web Application</span>
<span class="itemlist">Scripting Language</span>
<span class="itemlist">Andriod Application</span>
<span class="itemlist">Contact</span>
</nav>
</div>
<div style="padding:15px 15px 2500px;font-size:30px">
<p>Lorem ipsum dolor dummy text sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
To make the navigation bar scroll horizontally you need to give the containing element a width. i.e. add
#navigation {
width:100%;
}
also, you have
.scrolltab::-webkit-scrollbar {
display: none;
}
I am not sure whether you needed this for some reason, but on Microsoft Edge and Chrome at least it causes the scroll bar not to appear. Removing it renders the menu scrollable.

Display Image on Div/Text Overlay [duplicate]

How to align the tooltip the natural style: right bottom of the mouse pointer?
<!DOCTYPE html>
<html>
<head>
<title>Tooltip with Image</title>
<meta charset="UTF-8">
<style type="text/css">
.tooltip {
text-decoration:none;
position:relative;
}
.tooltip span {
display:none;
}
.tooltip span img {
float:left;
}
.tooltip:hover span {
display:block;
position:absolute;
overflow:hidden;
}
</style>
</head>
<body>
<a class="tooltip" href="http://www.google.com/">
Google
<span>
<img alt="" src="http://www.google.com/images/srpr/logo4w.png">
</span>
</a>
</body>
</html>
For default tooltip behavior simply add the title attribute. This can't contain images though.
<div title="regular tooltip">Hover me</div>
Before you clarified the question I did this up in pure JavaScript, hope you find it useful. The image will pop up and follow the mouse.
jsFiddle
JavaScript
var tooltipSpan = document.getElementById('tooltip-span');
window.onmousemove = function (e) {
var x = e.clientX,
y = e.clientY;
tooltipSpan.style.top = (y + 20) + 'px';
tooltipSpan.style.left = (x + 20) + 'px';
};
CSS
.tooltip span {
display:none;
}
.tooltip:hover span {
display:block;
position:fixed;
overflow:hidden;
}
Extending for multiple elements
One solution for multiple elements is to update all tooltip span's and setting them under the cursor on mouse move.
jsFiddle
var tooltips = document.querySelectorAll('.tooltip span');
window.onmousemove = function (e) {
var x = (e.clientX + 20) + 'px',
y = (e.clientY + 20) + 'px';
for (var i = 0; i < tooltips.length; i++) {
tooltips[i].style.top = y;
tooltips[i].style.left = x;
}
};
One way to do this without JS is to use the hover action to reveal a HTML element that is otherwise hidden, see this codepen:
http://codepen.io/c0un7z3r0/pen/LZWXEw
Note that the span that contains the tooltip content is relative to the parent li. The magic is here:
ul#list_of_thrones li > span{
display:none;
}
ul#list_of_thrones li:hover > span{
position: absolute;
display:block;
...
}
As you can see, the span is hidden unless the listitem is hovered over, thus revealing the span element, the span can contain as much html as you need. In the codepen attached I have also used a :after element for the arrow but that of course is entirely optional and has only been included in this example for cosmetic purposes.
I hope this helps, I felt compelled to post as all the other answers included JS solutions but the OP asked for a HTML/CSS only solution.
I prefer this technique:
function showTooltip(e) {
var tooltip = e.target.classList.contains("tooltip")
? e.target
: e.target.querySelector(":scope .tooltip");
tooltip.style.left =
(e.pageX + tooltip.clientWidth + 10 < document.body.clientWidth)
? (e.pageX + 10 + "px")
: (document.body.clientWidth + 5 - tooltip.clientWidth + "px");
tooltip.style.top =
(e.pageY + tooltip.clientHeight + 10 < document.body.clientHeight)
? (e.pageY + 10 + "px")
: (document.body.clientHeight + 5 - tooltip.clientHeight + "px");
}
var tooltips = document.querySelectorAll('.couponcode');
for(var i = 0; i < tooltips.length; i++) {
tooltips[i].addEventListener('mousemove', showTooltip);
}
.couponcode {
color: red;
cursor: pointer;
}
.couponcode:hover .tooltip {
display: block;
}
.tooltip {
position: absolute;
white-space: nowrap;
display: none;
background: #ffffcc;
border: 1px solid black;
padding: 5px;
z-index: 1000;
color: black;
}
Lorem ipsum dolor sit amet, <span class="couponcode">consectetur
adipiscing<span class="tooltip">This is a tooltip</span></span>
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in <span
class="couponcode">reprehenderit<span class="tooltip">This is
another tooltip</span></span> in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est <span
class="couponcode">laborum<span class="tooltip">This is yet
another tooltip</span></span>.
(see also this Fiddle)
You can use jQuery UI plugin, following are reference URLs
http://jqueryui.com/tooltip/
http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/
http://jquery-plugins.bassistance.de/tooltip/demo/
Set track to TRUE for Tooltip position relative to mouse pointer
eg.
$('.tooltip').tooltip({ track: true });
This CAN be done with pure html and css. It may not be the best way but we all have different limitations. There are 3 ways that could be useful depending on what your specific circumstances are.
The first involves overlaying an invisible table over top of your link with a hover action on each cell that displays an image.
#imagehover td:hover::after{
content: " ";
white-space: pre;
background-image: url("http://www.google.com/images/srpr/logo4w.png");
position: relative;
left: 5px;
top: 5px;
font-size: 20px;
background-color: transparent;
background-position: 0px 0px;
background-size: 60px 20px;
background-repeat: no-repeat;
}
#imagehover table, #imagehover th, #imagehover td {
border: 0px;
border-spacing: 0px;
}
<a href="https://www.google.com">
<table id="imagehover" style="width:50px;height:10px;z-index:9999;position:absolute" cellspacing="0">
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
</table>
Google</a>
The second relies on being able to provide your own cursor image
#googleLink{
cursor: url(data:application/octet-stream;base64,AAACAAEAICAAAAAAAACoEAAAF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D/1dXV/+Tk5P/k5OT/5OTk/+Tk5P8AAAD/AAAAADpI7zA3ReuqNUPq+jVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+vqNkTsgzdF8REAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAAAAAAAAA8SvEWNkTsZDZE66Q1Q+vSNUPr8DVD6v01Q+r6NkTr5zZE68Q3ReyQOEXtSj5M9QYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/8AD//8AAP/8AAA/+AAAH/AAAA/gAAAHwAAAB8AH4AOAH/gDgD/8AQB//gEA//4BAP4AAQH+AAEB/gABAf4AAQH+AAEB/gABAf4AAQD/AAEA////AH///4A///+AH/n/QAfg/wAAAH8AAAA/AAAAHwAAAB8AAAB/AQAA/wDAA/8=),auto;
}
Google
While the normal title tooltip doesn't technically allow images it does allow any unicode characters including block letters and emojis which may suit your needs.
Google
We can achieve the same using "Directive" in Angularjs.
//Bind mousemove event to the element which will show tooltip
$("#tooltip").mousemove(function(e) {
//find X & Y coodrinates
x = e.clientX,
y = e.clientY;
//Set tooltip position according to mouse position
tooltipSpan.style.top = (y + 20) + 'px';
tooltipSpan.style.left = (x + 20) + 'px';
});
You can check this post for further details.
http://www.ufthelp.com/2014/12/Tooltip-Directive-AngularJS.html
function showTooltip(e) {
var tooltip = e.target.classList.contains("tooltip")
? e.target
: e.target.querySelector(":scope .tooltip");
tooltip.style.left =
(e.clientX + tooltip.clientWidth + 10 < document.documentElement.clientWidth)
? (e.clientX + 10 + "px")
: (document.documentElement.clientWidth + 5 - tooltip.clientWidth + "px");
tooltip.style.top =
(e.clientY + tooltip.clientHeight + 10 < document.documentElement.clientHeight)
? (e.clientY + 10 + "px")
: (document.documentElement.clientHeight + 5 - tooltip.clientHeight + "px");
}
var tooltips = document.querySelectorAll('.hastip');
for(var i = 0; i < tooltips.length; i++) {
tooltips[i].addEventListener('mousemove', showTooltip);
}
.hastip {
border-bottom: 1px dotted black;
position: relative;
display: inline-block;
cursor: pointer;
}
.hastip:hover .tooltip {
display: block;
}
.tooltip {
position: fixed;
white-space: nowrap;
display: none;
background-color: #ffffcc;
border: 1px solid black;
color: #000;
padding: 5px;
border-radius: 6px;
z-index: 1000;
font-size: 13pt;
font-weight: initial;
}
Lorem ipsum dolor sit amet, <span class="hastip">consectetur
adipiscing<span class="tooltip">This is a tooltip</span></span>
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in <span
class="hastip">reprehenderit<span class="tooltip">This is
another tooltip</span></span> in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est <span
class="hastip">laborum<span class="tooltip">This is yet
another tooltip</span></span>.

lock height of div element to the size of its child elements

Basically I have three divs inside .main div displayed as flex. I'm trying to do basic swipe to get to next/previous divs. I can't figure out how to dynamically specify .main divs height according to its current child element's#wrapper height.
const main = document.querySelector('.main');
N = main.children.length;
let i = 0,
x0 = null,
locked = false,
w;
function unify(e) {
return e.changedTouches ? e.changedTouches[0] : e
};
function lock(e) {
x0 = unify(e).clientX;
main.classList.toggle('smooth', !(locked = true))
};
function drag(e) {
e.preventDefault();
if (locked)
main.style.setProperty('--tx', `${Math.round(unify(e).clientX - x0)}px`)
};
function move(e) {
if (locked) {
let dx = unify(e).clientX - x0,
s = Math.sign(dx),
f = +(s * dx / w).toFixed(2);
if ((i > 0 || s < 0) && (i < N - 1 || s > 0) && f > .2) {
main.style.setProperty('--i', i -= s);
f = 1 - f
}
main.style.setProperty('--tx', '0px');
main.style.setProperty('--f', f);
main.classList.toggle('smooth', !(locked = false));
x0 = null
}
};
function size() {
w = window.innerWidth
};
size();
main.style.setProperty('--n', N);
addEventListener('resize', size, false);
main.addEventListener('mousedown', lock, false);
main.addEventListener('touchstart', lock, false);
main.addEventListener('mousemove', drag, false);
main.addEventListener('touchmove', drag, false);
main.addEventListener('mouseup', move, false);
main.addEventListener('touchend', move, false);
.main {
--n: 1;
display: flex;
overflow-y: hidden;
width: 100%;
width: calc(var(--n)*100%);
/* height: 50vw; max-height: 100vh; */
transform: translate(calc(var(--tx, 0px) + var(--i, 0)/var(--n)*-100%));
}
#wrapper {
width: 100%;
width: calc(100%/var(--n));
user-select: none;
pointer-events: none
}
.smooth { transition: transform calc(var(--f, 1)*.3s) ease-out }
<div class="main">
<div id="wrapper">
<div>
<h1>Page 0</h1>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
</div>
<div id="wrapper">
<div>
<h1>Page 1 here!!!</h1>
</div>
</div>
<div id="wrapper">
<div>
<h1>page2 some stuff goes here!!!</h1>
</div>
</div>
</div>

How to scroll the circle using mouse wheel?

I have one circle and I have to rotate 360 deg using mouse scroll wheel or arrow but it will start when circle touches the blue border. Now my code is working when I scroll it and touch the blue border. But I have to scroll the circle using the mouse wheel. I mean using mouse wheel I have to rotate the circle also reverse.
Is there any idea how to do that?
$(window).bind('scroll', function() {
var l_360 = document.getElementById("l_360");
var controller = new ScrollMagic.Controller();
var tween = TweenLite.to(l_360, 3, {
x: -1910,
y: 350,
rotation: 360,
opacity: '1',
ease: Linear.easeOut
});
var scene = new ScrollMagic.Scene({
triggerElement: "#l_360"
})
.setTween(tween)
.setClassToggle('#l_360', 'fade-in')
.addTo(controller);
});
.parent_img {
position: fixed;
right: 0;
top: 0;
width: 100%;
text-align: right;
}
.content,
.content_2,
.content_3 {
width: 700px;
position: relative;
}
.content {
margin-top: 400px;
}
.main_l {
position: fixed;
right: 115px;
top: 20px;
z-index: 100;
}
.main_l .l_circle {
width: 250px;
height: 250px;
background-color: #EFBD40;
border-radius: 50%;
display: flex;
}
.main_l .l_circle h2 {
margin: auto;
color: #fff;
font-size: 35px;
}
.blue {
background-color: #0082ff;
transform: skew(0deg, -10deg);
z-index: -1;
/*color: #fff;*/
}
.blue .container {
transform: skew(0deg, 10deg);
position: relative;
top: 50px;
}
<div class="main_l" id="l_360">
<div class="l_circle">
<h2>360 circle</h2>
</div>
</div>
<div class="blue">
<div class="container">
<div class="content">
<h2>This is just for testing</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/TweenMax.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/plugins/ScrollToPlugin.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/plugins/CSSPlugin.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/ScrollMagic.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/plugins/animation.gsap.js"></script>
Using the wheel event will give you deltas for the direction you scroll the wheel. For example, to detect a vertical wheel movement the property deltaY will be either a negative value (forward) or positive (backward):
You can use the value directly if you wish, but it may vary depending on browser and system.
Example
var div = document.querySelector("div");
var angle = 0;
document.onwheel = function(e) {
if (e.deltaY) { // we have a wheel for vertical (common) direction
e.preventDefault();
angle += e.deltaY < 0 ? 4 : -4; // what direction?
div.style.transform = "rotate(" + angle + "deg)"; // do something
}
};
body {margin:30px}
div {border-radius:50%;border:3px solid #09f;width:150px;height:150px;}
<div> O</div>

How to Create a Link to a DIV

Here is what im working on... A maximizable and minimizable pop-up that is called by an hyperlink.
<html>
<style type="text/css">
.pop_out {
background: #333;
border-radius: 5px 5px 0 0;
box-shadow: 0px 0px 10px #000;
}
.minimized {
display: inline-block;
margin-right: 10px;
bottom: 0;
width: 250px;
height: 60px;
overflow: hidden;
}
.maximized {
top: 0;
position: fixed;
display: block;
width: auto;
height: auto;
/* Whatever styling you want when maximized, as long as you add the same styling to minimized class to change it back */
}
.close_pop {
cursor: pointer;
color: #fff;
}
.close_pop:hover {
color: red;
}
.expand_collapse {
margin-right: 10px;
cursor: pointer;
color: #fff;
height: 3px;
}
.expand_collapse:hover {
color: #ccc;
}
a {
position: fixed;
top: 150;
}
</style>
<script type="text/javascript">
var max = true;
function expand_collapse(elem) {
var top_div = elem.parentNode.parentNode.parentNode;
if (max === false) {
elem.innerHTML = "▼";
top_div.classList.toggle("minimized", false);
top_div.classList.toggle("maximized", true);
max = true;
} else if (top_div.classList.contains("maximized")) {
elem.innerHTML = "▲";
top_div.classList.toggle("minimized", true);
top_div.classList.toggle("maximized", false);
max = false
}
}
function close_pop(elem) {
var top_div = elem.parentNode.parentNode.parentNode;
top_div.style.display = 'none';
if (top_div.classList.contains("maximized")) {
max = false;
}
}
</script>
CLICK HERE<!--Right Here -->
<div style="position:fixed;bottom:0px;">
<div class="pop_out maximized">
<div style="padding:2px;position:relative;"> <span style="margin-left:10px;">Tab 1</span>
<span style="position:absolute;right:15px;">
<span class="expand_collapse" onclick="expand_collapse(this);">▼</span>
<span class="close_pop" onclick="close_pop(this);">&times</span></span>
</div>
<div style="background:white;font-size:15px;padding:2px;">The standard Lorem Ipsum passage, used since the 1500s "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.</div>
</div>
</div>
But the pop up opens with the page and i want the pop up to only be opened when the hyperlink commented is clicked and not when the page is loaded. Any help will be much appreciated. I have tried different methods on the hyperlink but to no avail.
Please check the link below
$(document).ready(function() {
$('.show-popup').on('click', function() {
$('.popup').fadeIn();
});
$('.close_pop').on('click', function() {
$('.popup').fadeOut();
});
});
.popup {
display: none;
}
.pop_out {
background: #333;
border-radius: 5px 5px 0 0;
box-shadow: 0px 0px 10px #000;
}
.minimized {
display: inline-block;
margin-right: 10px;
bottom: 0;
width: 250px;
height: 60px;
overflow: hidden;
}
.maximized {
top: 0;
position: fixed;
display: block;
width: auto;
height: auto;
/* Whatever styling you want when maximized, as long as you add the same styling to minimized class to change it back */
}
.close_pop {
cursor: pointer;
color: #fff;
}
.close_pop:hover {
color: red;
}
.expand_collapse {
margin-right: 10px;
cursor: pointer;
color: #fff;
height: 3px;
}
.expand_collapse:hover {
color: #ccc;
}
a {
position: fixed;
top: 150;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<a class="show-popup" href="#">CLICK HERE</a>
<!--Right Here -->
<div class="popup" style="position:fixed;bottom:0px;">
<div class="pop_out maximized">
<div style="padding:2px;position:relative;"> <span style="margin-left:10px;">Tab 1</span>
<span style="position:absolute;right:15px;">
<span class="expand_collapse" onclick="expand_collapse(this);">▼</span>
<span class="close_pop">&times</span></span>
</div>
<div style="background:white;font-size:15px;padding:2px;">The standard Lorem Ipsum passage, used since the 1500s "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
laborum.
</div>
</div>
</div>
The pop-up element isn't being hidden on initial page load.
Add style to the pop-up element in CSS.
display:none
Give the pop-up element an ID attribute for easier Javascript targeting
id="pop1"
Add attribute to the hyperlink
onclick="open_pop('#pop1')"
Add javascript function
function open_pop(elem)
{
elem.style.display = 'block';
}
Or to synchronise more effectively with your code, give the pop-up one of your custom classes to modify the display style.
I would also consider using ID's for Javascript selection as opposed to crawling through parents which could break the code if children elements or added or removed from the tree.
in simple code,
html
<div id="welcomeDiv" style="display:none;" class="answer_list" > WELCOME</div>
<input type="button" name="answer" value="Show Div" onclick="showDiv()" />
javascript
function showDiv() {
document.getElementById('welcomeDiv').style.display = "block";
}
I could not share my jsfiddle link. so this helps you i guess.
OR
Just make the div style = "display:none" on mouse click toggle the display.

Categories

Resources