I have an hidden element which contains a list. What I want to achieve is, to keep the hover state while moving from the clicked div with the cursor to the element with the list. The element which contains the list, should first disappear as soon as I go away from it e.g point away with the cursor. How can I achieve that?
I have this markup:
$('.hover').hover(function() {
$('.hoverDiv').addClass('show')
}, function() {
$('.hoverDiv').removeClass('show')
})
.container {
position: relative;
width: 100%;
height: 100%;
}
.hoverDiv {
display: none;
border: 1px solid red;
}
.hoverDiv.show {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="hover">
<p>
hover this div
</p>
</div>
<div class="hoverDiv">
show me when hovered
</div>
</div>
Here is the JSFIDDLE
You should bind the hover event to the .container instead of the .hover div. because when the user will move out from .hover, the list will be hide. But when user move on the .hoverDiv he still on the .container
$('.container').hover(function() {
$('.hoverDiv').addClass('show')
}, function() {
$('.hoverDiv').removeClass('show')
})
.container {
position: relative;
width: 100%;
height: 100%;
}
.hoverDiv {
display: none;
border: 1px solid red;
}
.hoverDiv.show {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="hover">
<p>
hover this div
</p>
<div class="hoverDiv">
show me when hovered
</div>
</div>
</div>
By the way, should don't need a script for this. You can do this using css only. Like this:
.container {
position: relative;
width: 100%;
height: 100%;
}
.hoverDiv {
display: none;
border: 1px solid red;
}
.container:hover .hoverDiv {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="hover">
<p>
hover this div
</p>
<div class="hoverDiv">
show me when hovered
</div>
</div>
</div>
Try this jquery code:
$('.hover').hover(function(){
$('.hoverDiv').addClass('show')
})
$('.hoverDiv').mouseleave(function(){
$('.hoverDiv').removeClass('show')
});
$('.hover').hover(function(){
$('.hoverDiv').addClass('show')
})
$('.hoverDiv').mouseleave(function(){
$('.hoverDiv').removeClass('show')
});
.container {
position: relative;
width: 100%;
height: 100%;
}
.hoverDiv {
display: none;
border: 1px solid red;
}
.hoverDiv.show {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="hover">
<p>
hover this div
</p>
</div>
<div class="hoverDiv">
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
</ul>
</div>
</div>
Related
In the below code, I want to hide the scrollbar of the first block (div1) without using overflow property in all the browsers.
Any suggestions would be helpful.
Fiddle:
http://jsfiddle.net/mvn1ngby/12/
$('#div1').scroll(function(){
$('#div2').scrollTop( $('#div1').scrollTop() );
});
$('#div2').scroll(function(){
$('#div1').scrollTop( $('#div2').scrollTop() );
});
div.outer {
display:inline-block;
width:150px;
height:320px;
border:1px solid black;
overflow-y:auto;
}
div.outer > div {
height:3000px;
}
#div1 div {
width:300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer" id="div1">
<div>
</div>
</div>
<div class="outer" id="div2">
<div>
</div>
</div>
It is a hack but works.
The idea is to pull the area of the scroll-bar outside of the view port.
The "pull" size suppose to be with the width of the scroll bar, usually the wider one (on Windows)
$('#div1').scroll(function() {
$('#div2').scrollTop($('#div1').scrollTop());
});
$('#div2').scroll(function() {
$('#div1').scrollTop($('#div2').scrollTop());
});
div.outer {
display: inline-block;
overflow: hidden;
border: 1px solid black;
}
#div1>div,
#div2>div {
height: 3000px;
}
.scrollable {
width: 150px;
height: 320px;
overflow-y: auto;
}
#div1 {
margin-right: -25px;
padding-right: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer">
<div class="scrollable" id="div1">
<div></div>
</div>
</div>
<div class="outer">
<div class="scrollable" id="div2">
<div></div>
</div>
</div>
Try to add a new container div with css:
.container { width: 100%;}
And inside put the div1, div2
I am trying to use links to scroll the content within a div.
HTML is here:
<header>
<div class="wrapper">
<img src="/img/header.jpg" alt="Ace Land Surveying">
</div>
<nav>
<div class="wrapper">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Types of Surveys</li>
<li>About Us</li>
<li>Links</li>
<li>Request a Survey</li>
<li>Past Projects</li>
<li>Contact</li>
<li>SOQ</li>
</ul>
</div>
</nav>
</header>
<div class="wrapper">
<div class="content">
<div class="column left">
<ul class="survey-type-list">
<li><h2>CLICK HERE</h2></li>
<li><h2>CLICK HERE</h2></li>
<li><h2>CLICK HERE</h2></li>
</ul>
</div>
<div class="column right" id="survey-column">
<div class="survey-type" id="type1">
<p> ... long text ... </p>
</div>
<div class="survey-type" id="type2">
<p> ... long text ... </p>
</div>
<div class="survey-type" id="type3">
<p> ... long text ... </p>
</div>
</div>
</div>
</div>
<footer>
<div class="footer-top">
<div class="wrapper"></div>
</div>
</footer>
and the CSS:
header {
float: left;
width: 100%;
height: 200px;
}
.wrapper {
width: 90%;
margin: auto;
}
.content {
float: left;
width: 100%;
padding: 20px;
background: #02274b;
}
.column {
min-height: 500px;
padding: 20px;
border-radius: 20px;
color: #fff;
background-image: -webkit-linear-gradient(top, rgba(255, 255, 255, 0.3), transparent);
background: linear-gradient(to bottom, rgba(255, 255, 255, 0.3), transparent);
}
.column.left {
float: left;
width: 20%;
}
.column.right {
float: right;
width: 60%;
}
.survey-type-list li {
margin: 0 0 4px 0;
list-style: none;
}
.survey-type-list h2 {
font-size: 13px;
}
#survey-column {
overflow: hidden;
height: 460px;
}
.survey-type {
float: left;
width: 100%;
height: 460px;
}
The problem is that the entire page moves with the content inside the div.
Here is a fiddle:
http://jsfiddle.net/q8a1s5wj/8/
I tried several other threads here but none could solve my problem.
How can I prevent the whole page from scrolling and just scroll inside the right column with my anchor links?
I want to be able to click the link in the left column and see the right column scroll but not the move the entire page.
Fiddle
Some minor adaptations to the CSS - not doing this gave the wrong element offset :
#survey-column {
position: relative;
}
This one's just so the last div can scroll to the top completely :
.survey-type {
height: 520px;
}
And a bit of script to make it work :
$(function() {
$('.column.left a').click(function() {
var goal = $(this.hash).position().top-20,
aim = goal+$('#survey-column').scrollTop();
$('#survey-column').scrollTop(aim);
return false;
});
});
The 20 pixels deduction of goal is just done to keep the same top padding as was started with...
you can add position as fixed on top div element, i have checked your code on jsfiddle and added position style attribute in wrapper div element
<div class="wrapper" style="position:fixed; margin-top:10px">
just add this
.stop-scrolling {
height: 100%;
overflow: hidden;
}
this class to your body tag.
Reference: How to disable scrolling temporarily?
Updated Fiddle: http://jsfiddle.net/q8a1s5wj/2/
I am going to add dynamically elements to my block of ul.
I would like to center all list's elements to parent div(brown boder).
For example,
if the resolution of the browser allows you to set two blocks in one row, I would like to center this row in relation to parent div.
I would be very graftefully.
Link to demo
myCode:
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
var tab = [2,3,4,5,7,8,9,11,12,13,14,15];
$(document).ready(function(){
$('#godziny').on('click', '.godzina', function(){
//alert(this.attr('class'));
$('.yb').removeClass('yb');
$(this).addClass('yb');
});
$('#getElements').click(function() {
for(i = 0; i < tab.length; ++i) {
alert(tab[i]);
setTimeout(function(i){
$('#godziny').append('<li class="godzina">' + tab[i] + '</li>');
}, i*50);
}
});
});
</script>
<style>
#spisSalonow {
margin: 0 auto;
}
#spisSalonow > div {
padding-top: 15px;
color:red;
}
#wybor_terminu {
border: 1px solid brown;
}
#wybor_terminu ul {
list-style-type: none;
overflow: hidden;
border: 1px solid red;
}
#wybor_terminu ul li {
width: 200px;
height: 200px;
text-align: center;
color: blue;
border: 0.2em solid green;
float: left;
cursor: pointer;
margin-right: 40px;
margin-top: 40px;
/*margin:auto;*/
/*
opacity: 0.4;
filter: alpha(opacity=40);
*/
}
.yb {
background: yellow;
}
</style>
</head>
<body>
<div id="wrapper">
<input type="button" value="get Elements" id="getElements"/>
<section id="content">
<div class="full">
<BR/>
<div id="wybor_terminu" class="center border" style="width: 70%; position: relative;">
<div style="text-align: center"><img src="https://cdn0.iconfinder.com/data/icons/slim-square-icons-basics/100/basics-05-24.png" alt="Left Arrow" /> <span id="day"> ANY DAY </span> <img src="http://cdn0.iconfinder.com/data/icons/slim-square-icons-basics/100/basics-06-24.png" alt="Right Arrow" /></div>
<ul id="godziny" style="margin-top: 25px;">
</ul>
</div>
</section>
</div>
</body>
</html>
You can use the CSS flexbox to achieve this. Here is a link to a complete guide on how to use flexbox. I hope this helps.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Add this lines:
CSS
#wybor_terminu ul {
list-style-type: none;
overflow: hidden;
/*NEW*/
padding: 0;
width: 100%;
}
#wybor_terminu ul li {
width: 200px;
height: 200px;
text-align: center;
color: blue;
border: 0.2em solid green;
/*float: left; You don't need this line*/
cursor: pointer;
/*NEW*/
margin:auto;
margin-top: 40px;
}
EDIT
This is only a quick solution with bootstrap maybe it could help you a little bit. jsfiddle
jQuery
In this line I added bootstrap classes:
$('#godziny').append('<li class="godzina col-sm-12 col-md-6">' + tab[i] + '</li>');
This code center your boxes (is not the best solution, but it works):
countBoxes = $('#godziny').width() / 200;
alignBoxes = ($('#godziny').width()-(200*parseInt(countBoxes)))/2;
if(countBoxes >= 2.65){
$('#godziny').css('margin-left', alignBoxes);
} else{
$('#godziny').css('margin-left', 0);
}
If you change the resolution of your screen, click the button to center your boxes again.
I’m having a little trouble with this template: basically, I’m trying to add functionality where if you click a box it will expand sliding the other ones off-screen, but instead sliding the div off-screen it’s disappearing completely.
Here is what I have so far: JSFiddle.
$(function() {
$(".box").click(function() {
var isopened = $(this).attr("isopen");
if (isopened == "true") {
$(this).css("position", "relative").css("width", $(this).attr("data-ow"));
$(this).attr("isopen", "false");
}
else {
$(this).attr("data-ow", $(this).css("width"));
$(this).css("position", "relative").css("width", "40%");
$(this).attr("isopen", "true");
}
});
});
* {
margin: 0;
padding: 0;
}
.container {
width: 100%;
max-width: 100%;
max-height: 600px;
overflow: hidden;
}
.box {
height: 600px;
display: block;
width: 13.33333333%;
border: 1px solid white;
background-color: black;
float: right;
position: relative;
}
.box:first-of-type {
width: 29.0%;
background-color: orange;
}
.box:last-of-type {
width: 29.0%;
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
What I ultimately want is when one of the boxes is clicked it expands and instead of the entire div being hidden only the part which is off-screen is hidden:
I think you might like this flexbox solution as you can do what you want without usign any jQuery/JS. Pure CSS and HTML:
body {
background-color: black
}
#container {
display: flex;
width: 100%;
height: 50vh;
}
#container > div {
flex: 1;
min-width: 0;
transition:min-width 0.2s ease;
outline:0;
}
#container > div:focus {
min-width: 50vw;
}
<div id="container">
<div tabindex="0" style="background-color:blue"></div>
<div tabindex="0" style="background-color:orange"></div>
<div tabindex="0" style="background-color:green"></div>
<div tabindex="0" style="background-color:white"></div>
<div tabindex="0" style="background-color:blue"></div>
</div>
I used tabindex to give me the ability to use the :focus selector.
I want to pop up a menu on click of a div and I got it all working in this Fiddle: http://jsfiddle.net/EhtrR/825/ but I cant manage to make it work on my code.
HTML:
<div id="clickOne" class="clickDesign">
<h2 class="fs20 nobold">Leafy Plants</h2>
</div>
<div id="clickTwo" class="clickDesign">
<h2 class="fs20 nobold">Juicy Plants</h2>
</div>
</div>
<div id="leafyPlants">
Leafy Test
</div>
<div id="juicyPlants">
Juicy Test
</div>
CSS:
#leafyPlants{
display:none;
position:absolute;
top:50px;
}
#juicyPlants{
display:none;
position:absolute;
top:50px;
}
jQuery:
$("#clickOne").on('click', function() {
$("#leafyPlants").fadeIn();
$("#juicyPlants").fadeOut();
});
$("#clickTwo").on('click', function() {
$("#leafyPlants").fadeOut();
$("#juicyPlants").fadeIn();
});
It doesn't show anything when I put it my code.
Add this css
<style>
img {
float: left;
display: block;
height: 40px;
width: 40px;
cursor: pointer;
}
#img1 {
background: #b00;
}
#img2 {
background: #c00;
}
#div1 {
display: none;
position: absolute;
top: 50px;
width: 200px;
background: #077;
left: 10px;
}
#div2 {
display: none;
position: absolute;
top: 50px;
width: 200px;
background: #0b0;
left: 10px;
}
br {
clear: both;
}
</style>
This js codes
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
<script>
$(document).ready(function(e) {
$("#img1").on('click', function() {
$("#div1").fadeIn();
$("#div2").fadeOut();
});
$("#img2").on('click', function() {
$("#div1").fadeOut();
$("#div2").fadeIn();
});
});
</script>
And you HTML
<img id="img1"/> <img id="img2"/> <br/>
<div id="div1">1</div>
<div id="div2">2</div>
It should work. Most probably you did not included jQuery library. I added it.
<script src="http://code.jquery.com/jquery-1.11.0.js"></script>
Using it should solve your porblem probably.