So, I have 3 flexbox containers of the same dimension. When I click on one of them, it should stretch to become bigger then the others, and then back to normal if I click on it again.
I've made it using JS, and it works.
The problem is that when I already have one the boxes stretched and I try to stretch another one, the transition duration increases. And I can't figure out why. I thought the different transitions would start simultaneously.
This is the code:
/* HTML */
<div class="flexbox">
<div class="box-1" onclick="box1();">
<h3>Box 1</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore</p>
</div>
<div class="box-2" onclick="box2();">
<h3>Box 2</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore</p>
</div>
<div class="box-3" onclick="box3();">
<h3>Box 3</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore</p>
</div>
</div>
/* CSS */
.flexbox {
display: flex;
width: auto;
justify-content: center;
}
.flexbox div {
border: 1px #ccc solid;
border-radius: 10px;
width: 250px;
height: 250px;
padding: 10px;
margin: 2px 10px;
transition: flex-grow 1s linear 0s;
}
.box-1{
flex-grow: 0;
}
.box-2 {
flex-grow: 0;
}
.box-3 {
flex-grow: 0;
}
/* JS */
function box1(){
const box1 = document.getElementsByClassName('box-1')[0];
if(box1.style.flexGrow != '4'){
box1.style.flexGrow = '4';
} else {
box1.style.flexGrow = '0';
}
const box2 = document.getElementsByClassName('box-2')[0];
box2.style.flexGrow = '0';
const box3 = document.getElementsByClassName('box-3')[0];
box3.style.flexGrow = '0';
}
function box2(){
const box2 = document.getElementsByClassName('box-2')[0];
if(box2.style.flexGrow != '4'){
box2.style.flexGrow = '4';
} else {
box2.style.flexGrow = '0';
}
const box1 = document.getElementsByClassName('box-1')[0];
box1.style.flexGrow = '0';
const box3 = document.getElementsByClassName('box-3')[0];
box3.style.flexGrow = '0';
}
function box3(){
const box3 = document.getElementsByClassName('box-3')[0];
if(box3.style.flexGrow != '4'){
box3.style.flexGrow = '4';
} else {
box3.style.flexGrow = '0';
}
const box1 = document.getElementsByClassName('box-1')[0];
box1.style.flexGrow = '0';
const box2 = document.getElementsByClassName('box-2')[0];
box2.style.flexGrow = '0';
}
How can I have the transition duration to be always the same (1s in this example)?
There are a number of improvements that can be made to your code that will reduce duplication, avoid complications of interacting with the live HTMLCollection that getElementsByClassName returns, allow you to avoid using inline onclick assignments, and will cause the transition to only take 1 second.
The example below implements a single grow() function which is called by listeners attached to each box. The listeners are added by iterating over the NodeList returned by querying the DOM with .querySelectorAll('.box') and calling addEventListener() on each element.
In the CSS we declare classes for each state we want our boxes to be able to have - in this case flex-grow: 0 and flex-grow:4. Then in our grow() function we simply add or remove these styles as necessary based on which box was clicked.
function grow(e){
// if the clicked box already has class 'flexgrow4' return
if (e.currentTarget.classList.contains('flexgrow4')) return;
// otherwise find the element in the flexbox container that has
// class 'flexgrow4' and replace it with 'flexgrow0'
const lastActive = flexbox.querySelector('.flexgrow4');
if (lastActive) lastActive.classList.replace('flexgrow4', 'flexgrow0');
// finally, replace 'flexgrow0' with 'flexgrow4' on the clicked box
e.currentTarget.classList.replace('flexgrow0', 'flexgrow4');
}
const flexbox = document.querySelector('.flexbox');
const boxes = document.querySelectorAll('.box')
boxes.forEach(box => box.addEventListener("click", grow, false));
.flexbox {
display: flex;
width: 100vw;
justify-content: center;
}
.flexbox div {
border: 1px #ccc solid;
border-radius: 10px;
width: 100px;
height: 250px;
padding: 10px;
margin: 2px 10px;
transition: all 1s linear 0s;
}
.flexgrow0 {
flex-grow: 1;
}
.flexgrow4 {
flex-grow: 4;
background-color:tomato;
}
<div class="flexbox">
<div class="box flexgrow0">
<h3>Box 1</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore</p>
</div>
<div class="box flexgrow0">
<h3>Box 2</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore</p>
</div>
<div class="box flexgrow0">
<h3>Box 3</h3>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore</p>
</div>
</div>
Related
I coded this:
$("#scroll-to-left-button").on("mousedown", function() {
var x = $("#scroll-area").scrollLeft();
$("#scroll-area").scrollLeft(x - 10);
});
$("#scroll-to-right-button").on("mousedown", function() {
var x = $("#scroll-area").scrollLeft();
$("#scroll-area").scrollLeft(x + 10);
});
#container {
width: 50%;
height: 100px;
background-color: grey;
position: relative;
}
#scroll-area {
white-space: nowrap;
overflow-x: auto;
height: 100%;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="container">
<div id="scroll-area">
<div id="text">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</div>
</div>
</div>
<button id="scroll-to-left-button">Scroll to left</button>
<button id="scroll-to-right-button">Scroll to right</button>
You need to click the buttons pretty often to navigate through this container. Is there a way to let it based on the duration of the mouse press? Like if you keep the mouse pressed, it continues constantly scrolling? And if you stop, it stops.
Would be happy if someone could help me.
Here's a working solution. Also your code was a bit wet, so I refactored it a bit. You only need one mousedown event listener.
let interval;
$('.scroll-btn').on('mousedown', ({ target }) => {
const type = $(target).attr('id');
interval = setInterval(() => {
var x = $('#scroll-area').scrollLeft();
$('#scroll-area').scrollLeft(type === 'left' ? x - 10 : x + 10);
}, 50);
});
$('.scroll-btn').on('mouseout mouseup', () => clearInterval(interval));
#container {
width: 50%;
height: 100px;
background-color: grey;
position: relative;
}
#scroll-area {
white-space: nowrap;
overflow-x: auto;
height: 100%;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="container">
<div id="scroll-area">
<div id="text">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua.
</div>
</div>
</div>
<button id="left" class="scroll-btn">Scroll Left</button>
<button id="right" class="scroll-btn">Scroll Right</button>
Well, the mousedown and mouseup make a good pair, although you have used only mousedown :)
Here's a sample how it could be done.
Note that there're couple other things that could be done to this code for it to look nicer:
.on(... is not probably needed, you could just write it as .mousedown(...
the code for the right and left buttons look really similar, you could unite these blocks in one and distinguish by an additional attrubute (let's say like move="10" for the right button and move="-10" for the left one, and then just getting this value in order to add it to scrollLeft)
var tmr;
$(".scroll-button").mousedown(function() {
//let's setup the timer here...
move = +$(this).attr("move");
tmr = setInterval(function(){
$("#scroll-area")[0].scrollLeft+=move;
}, 250)
});
$(".scroll-button").mouseup(function() {
// and destroy the timer here
clearInterval(tmr);
});
#container {
width: 50%;
height: 300px;
background-color: grey;
position: relative;
}
#scroll-area {
white-space: nowrap;
overflow-x: auto;
height: 100%;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="container">
<div id="scroll-area">
<div id="text">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</div>
</div>
</div>
<button class="scroll-button" move="-10">Scroll to left</button>
<button class="scroll-button" move="10">Scroll to right</button>
I'm currently working on an image slider for my company's new homepage and just can't figure out the error that seems to lie in the javascript part ..
The slider contains 4 images and should, as soon as the last image is reached, begin at the first image again. The problem is that the images are displayed in that order "1-2-1-2-3-4-1-2-3-4..."
Here the code:
$('.slide').first().addClass('active');
$('.slide').hide();
$('.active').show();
$('#next').on('click', nextSlide);
$('#prev').on('click', prevSlide);
// Auto slider
if (options.autoswitch === true) {
setInterval(nextSlide, options.autoswitch_speed);
}
function nextSlide() {
$('.active').removeClass('active').addClass('prevActive');
if ($('.prevActive').is(':last-child')) {
$('.slide').first().addClass('active');
} else {
$('.prevActive').next().addClass('active');
}
$('.prevActive').removeClass('prevActive');
$('.slide').fadeOut(options.speed);
$('.active').fadeIn(options.speed);
}
function prevSlide() {
$('.active').removeClass('active').addClass('prevActive');
if ($('.prevActive').is(':first-child')) {
$('.slide').last().addClass('active');
} else {
$('.prevActive').prev().addClass('active');
}
$('.prevActive').removeClass('prevActive');
$('.slide').fadeOut(options.speed);
$('.active').fadeIn(options.speed);
}
});
and the corresponding CSS:
#slider-container {
height: 600px;
margin: 0 auto;
max-width: 100%;
overflow: hidden;
position: relative;
width: 800px;
}
#sldier {
height: 100%;
width: 100%;
}
#slider .slide img {
height: 100%;
width: auto;
}
#prev, #next {
cursor: pointer;
max-width: 30px;
opacity: 1;
position: absolute;
top: 8%;
-webkit-transition: opacity 0.2s linear;
-moz-transition: opacity 0.2s linear;
-o-transition: opacity 0.2s linear;
transition: opacity 0.2s linear;
z-index: 999;
}
#prev { left: 12px; }
#next { right: 3px; }
#slider-container:hover #prev, #slider-container:hover #next { opacity: .7; }
.slide {
position: absolute;
width: 100%;
}
.slide-copy {
background: #777;
background: rgba(0, 0, 0, .5);
bottom: 0;
color: #fff;
left: 0;
padding: 20px;
position: absolute;
}
#media (min-width: 600px) {
#prev, #next {
top: 45%;
}
}
and the HTML
<div id="slider-container">
<img src="img/arrowprev" id="prev" alt="prev">
<ul id="slider">
<li class="slide">
<div class="slide-copy">
<h2>Placeholder</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.</p>
</div>
<img src="img/placeholder" alt="placeholder">
</li>
<li class="slide">
<div class="slide-copy">
<h2>Placeholder2</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.</p>
</div>
<img src="img/placeholder" alt="placeholder">
</li>
<li class="slide">
<div class="slide-copy">
<h2>Placeholder3</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.</p>
</div>
<img src="img/placeholder" alt="placeholder">
</li>
<li class="slide">
<div class="slide-copy">
<h2>Placeholder4</h2>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.</p>
</div>
<img src="img/placeholder" alt="placeholder">
</li>
</ul>
<img src="img/arrownext" id="next" alt="next">
</div>
Maybe it has something to do with
if ($('.prevActive').is(':first-child')) {
$('.slide').last().addClass('active');
I just can't figure out the problem and would appreciate any help.
Thank you in advance :)
This is the page I took as source:
https://www.jqueryscript.net/slider/Tiny-jQuery-Image-Slider-Slideshow-With-Caption-Support.html
I found the fail, the unique thing it wasn't working, is the apply of visibility on each prevActive and active elements on each next/prev actions, so I've added this lines to both functions:
$('.prevActive').hide();
$('.active').show();
(Also I've commented the lines referencing to options variable, as is not defined in the description).
You can see the fiddle working here:
https://jsfiddle.net/a4bssrf5/8/
I am applying for a job and need a resume. Because I really hate working with Word and it drives me nuts using it for such a purpose I just did it with HTML and CSS, thinking it would be fairly easy to export it to pdf later.
But well, it turned out to be harder than expected. I have tried using jsPDF and pretty much every method decribed in posts on stackoverflow regarding this subject, but it somehow was always distorted or did not work at all.
Either there was no css included or there was only a part of my resume in the pdf.
I have attached the files and need the .page to be exported as pdf, the format should be A4 already.
Is there a way to achieve what I need using JavaScript/jQuery ?
In the worst case scenario it would also be possible for me to use php, but that would make things way more complicated for me.
I know it probably would be easiest to just do it in Word or something like that regarding time consumption. But just from personal interest how would I have to do it ?
Thank you guys very much in advance
*{
margin: 0;
padding: 0;
font-family: 'Open Sans';
}
body {
background:#aaa;
}
.dash {
content: '';
width: 100%;
height: 1px;
background: #676767;
display: block;
clear:both;
}
.page {
width: 1000px;
height: 1414.2135px;
background: white;
margin: 100px auto;
position: relative;
overflow: hidden;
}
.page .overlay #left_rect {
width: 200%;
height: 500px;
background: #676767;
position: absolute;
z-index: 1;
transform: rotate(-45deg);
left: -100%;
top: -50px;
}
.page .overlay #right_rect {
width: 200%;
height: 500px;
background: #26556d;
position: absolute;
transform: rotate(16.18deg);
z-index: 2;
left: -100px;
top: -249px;
}
.page .left,
.page .right {
float: left;
}
.page .left {
width: calc(38.1966% - 50px);
height: 100%;
background: #eee;
padding: 565px 25px 0;
}
.page .left img {
width: 250px;
position: absolute;
left: 60px;
top: 190px;
z-index: 2;
}
.page .left .section {
margin-bottom: 25px;
}
.page .left .section.contact {
position: absolute;
left: 25px;
bottom: 0;
}
.page .right {
width: calc( 61.8034% - 50px);
height: 100%;
margin-top: 200px;
padding: 25px;
}
.page .right .top h1 {
color: #255571;
font-size: 35px;
float:left;
}
.page .right .top h2 {
color: #666;
font-size: 20px;
float: left;
margin-top: 15px;
margin-left: 10px;
font-weight: 400;
}
.page .right .top:after {
content:'';
display: block;
clear: both;
height: 25px;
}
.page .right .section:after {
content:'';
display: block;
clear: both;
height: 25px;
}
.page .section h1 {
color: #26556d;
font-size: 25px;
text-transform: uppercase;
}
.page .right .section .sub {
padding: 5px 0 10px;
}
.page .right .section .sub.half {
width: 50%;
float: left;
}
.page .right .section .sub h2 {
color: #676767;
font-size: 18px;
}
.page .right .section .sub h3 {
color: #676767;
font-size: 18px;
font-weight: 400;
margin-bottom: 5px;
}
.page .right .section .sub h4,
.page .left .section p {
color: #777;
font-size: 17px;
font-weight: 400;
}
.page .right .section .sub ul {
margin-left: 30px;
color: #777;
font-size: 17px;
font-weight: 400;
}
<!DOCTYPE HTML>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
</head>
<body>
<section class="page">
<div class="overlay">
<div id="left_rect"></div>
<div id="right_rect"></div>
</div>
<div class="left">
<img src="https://cdn.pixabay.com/photo/2015/12/22/04/00/photo-1103597_1280.png">
<div class="section about">
<h1>About Me</h1>
<p>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</p>
</div>
<div class="section contact">
<h1>Contact</h1>
<p>
John Doe<br>
Some Street 123<br>
1234 City<br><br>
<b>T:</b> 01234 5678910<br>
<b>M:</b> john.doe#mail.com
</p>
</div>
</div>
<div class="right">
<div class="top">
<h1>John Doe</h1>
<h2>01.01.1990</h2>
</div>
<div class="section">
<h1>Lorem Ipsum</h1>
<div class="sub half">
<h2>School (2,0)</h2>
<h3>Sep 2000 - Aug 2015</h3>
<h4>Some School</h4>
</div>
<div class="sub half">
<h2>B.Sc. Business</h2>
<h3>Sep 2016 - Mar 2019</h3>
<h4>Some University</h4>
</div>
</div>
<div class="section">
<h1>Lorem Ipsum</h1>
<div class="sub">
<h2>Softwaredeveloper at some company</h2>
<h3>Aug 2015 - Mar 2016</h3>
<ul>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
</ul>
</div>
<span class="dash"></span>
<div class="sub">
<h2>Softwaredeveloper at some company</h2>
<h3>Apr 2016 - Feb 2017</h3>
<ul>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
</ul>
</div>
<span class="dash"></span>
<div class="sub">
<h2>Softwaredeveloper at some company</h2>
<h3>Mar 2017 - Mar 2018</h3>
<ul>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr</li>
</ul>
</div>
</div>
<div class="section">
<h1>Lorem Ipsum</h1>
<div class="sub half">
<h2>IT-Knowledge</h2>
<ul>
<li>PHP, Java, JavaScript</li>
<li>HTML, CSS, jQuery, MySQL</li>
<li>Operating Systems: Windows, MacOS</li>
</ul>
</div>
<div class="sub half">
<h2>Languages</h2>
<ul>
<li>German</li>
<li>English</li>
<li>French</li>
<li>Spanish</li>
</ul>
</div>
<span class="dash"></span>
<div class="sub">
<h2>Interests</h2>
<ul>
<li>Lorem ipsum dolor sit amet, consetetur sadipscing elitr:</li>
<ul>
<li>Sed diam nonumy eirmod tempor invidunt</li>
<li>Sed diam nonumy eirmod tempor invidunt</li>
<li>Sed diam nonumy eirmod tempor invidunt</li>
</ul>
<li>
Sed diam nonumy eirmod tempor invidunt
</li>
</ul>
</div>
</div>
</div>
</section>
</body>
</html>
Everything is working as expected.
For redability, html to pdf ignores background images and defaults the text color to system color (which is usually black). Why? Because in most cases it does more good than harm. Web graphics can use a wide array of graphic techniques and not all are compatible with print (in the sense it might come out looking bad, being hard to read and it might also be heavier on ink cartridges). Most people printing a web page are interested in the text and its readability, not in the fancy graphics.
So, when trying to Print (Ctrl + P) your CV directly from the SO snippet you placed in your question, by default, Chrome ignored background graphics. After I opened advanced settings and checked "Background graphics" it looked as you want it to:
I have no idea how to enable background graphics in whatever library you're trying to use, but I can assure you most (if not all) have a setting for it that's turned off by default.
All you need to do is find the setting in your library's docs and turn it on.
As I understand you are looking a pdf file with CSS. we can do it using xeponlineformatter
please refer below link.
http://www.cloudformatter.com/CSS2Pdf
here you can see a download button, under this button you can save page as pdf.
it will saving your page with appropriate css.
hope this will work for you.
I don't know how to do this with JavaScript/jQuery, but you could try using a software like doPDF which installs you a virtual printer driver, then you print as you would do exactly to a regular printer, just the result will be a PDF file.
If you go for the PHP way, i recommend you Dompdf
When hovering an element a div will be shown. I have a couple of this elements. So each div has an unique idand each it's own height. To align the div-class next to the cursor I need to know its individual height.
Here is an extraction of what I got (please note the lines that I have marked with ****):
$('.rsshover').mouseleave(function() {
var id = $(this).attr('id').replace("did_", "");
$("#pre_"+id).hide();
});
// cache the selector
var follower = $(".preview");
var IDHeight = ****???****
var xp = 0, yp = 0;
var loop = setInterval(function(){
xp += (mouseX + 15 - xp) / 12;
yp += (mouseY - ****IDHeight**** - yp) / 12;
follower.css({left:xp, top:yp});
}, 0);
So what I would like to archieve can be seen in that fiddle and when entering a value for a specific height like yp += (mouseY - 200 - yp) / 12;.
The aelements are placed on the bottom of the page. So the hidden divs need to grow upwards what means that the starting reference-point of the div should be the left-bottom-edge and instead building it up downwards it needs to grow upwards.
So I have no clue how to solve this. Would appreciate if there is someone who could help me out. Thanks in advance.
Here is what it should look like and what I want to achieve. I needed to enter two different heights manually by hand in that line yp += (mouseY - **HEIGHTVALUE** - yp) / 12;
Since you're already using jquery, you can get height of any div by $('#someid').height();
The below example provides the functionality you seek.
var mouseX = 0, mouseY = 0, limitX = 320, limitY = 15;
$('.alink').mousemove(function(e){
var id = $(this).attr('id').replace("id_", "");
// get the current hidden div selector
var follower = $("#div_to_id_"+id);
follower.show();
// change 12 to alter damping higher is slower
follower.animate({
left: e.pageX,
top: e.pageY-(follower.height()+30),
height: "auto"
}, 10, function() {
});
});
$('.alink').mouseleave(function() {
$(".hiddendivs").hide();
});
a {
display: inline-block;
width:320px;
height:15px;
position:relative;
margin-top: 150px;
background-color: red;
}
.hiddendivs {
background: green;
color: #fff;
width: 310px;
padding: 15px;
position:fixed;
z-index: 1;
display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" class="alink" id="id_1">
<div class="hiddendivs" id="div_to_id_1">
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod temp...</p>
</div>
</a>
<a href="#" class="alink" id="id_2">
<div class="hiddendivs" id="div_to_id_2">
<p>some other content...</p>
</div>
</a>
Updated Fiddle
I found this code: link
$(".show-more a").on("click", function() {
var $this = $(this);
var $content = $this.parent().prev("div.content");
var linkText = $this.text().toUpperCase();
if(linkText === "SHOW MORE"){
linkText = "Show less";
$content.switchClass("hideContent", "showContent", 400);
} else {
linkText = "Show more";
$content.switchClass("showContent", "hideContent", 400);
};
$this.text(linkText);
});
CSS:
div.text-container {
margin: 0 auto;
width: 75%;
}
.hideContent {
overflow: hidden;
line-height: 1em;
height: 2em;
}
.showContent {
line-height: 1em;
height: auto;
}
.showContent{
height: auto;
}
h1 {
font-size: 24px;
}
p {
padding: 10px 0;
}
.show-more {
padding: 10px 0;
text-align: center;
}
It was exactly what I was looking for, but as you can see here, if you modify it (link), the "Show more" link is there if you have only one or two lines, and it is not needed in that case.
Thank you for your answers!
As your sample code was not fully working I decided to enhance one of my own samples I created in a post a while ago.
DEMO - Show more/less and hide the link when not needed
The demo shows the first text to have no link and the second to have a link. If you add a few more characters to the first text you see the link appear when you run the fiddle again.
The idea is to double check the client vs the actual height and determine then if you want to show the link. Similar to the below.
$(".show-more a").each(function() {
var $link = $(this);
var $content = $link.parent().prev("div.text-content");
console.log($link);
var visibleHeight = $content[0].clientHeight;
var actualHide = $content[0].scrollHeight - 1; // -1 is needed in this example or you get a 1-line offset.
console.log(actualHide);
console.log(visibleHeight);
if (actualHide > visibleHeight) {
$link.show();
} else {
$link.hide();
}
});
The demo is using the following HTML:
<div class="text-container">
<h1>Lorem ipsum dolor</h1>
<div class="text-content short-text">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
labore Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt
</div>
<div class="show-more">
Show more
</div>
</div>
<div class="text-container">
<h1>Lorem ipsum dolor</h1>
<div class="text-content short-text">
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
</div>
<div class="show-more">
Show more
</div>
</div>
and the following basic CSS:
div.text-container {
margin: 0 auto;
width: 75%;
}
.text-content{
line-height: 1em;
}
.short-text {
overflow: hidden;
height: 2em;
}
.full-text{
height: auto;
}
h1 {
font-size: 24px;
}
.show-more {
padding: 10px 0;
text-align: center;
}
See the working fiddle here - http://jsfiddle.net/tariqulazam/hpeyH/
First you have to measure if the content has overflowed or not. I have used the solution from detect elements overflow using jquery.
Finally use this plugin to decide whether to show or hide the 'show more' link.
$("div.content").HasScrollBar() ==true ? $(".show-more").show():$(".show-more").hide();
I don't know whats your real question is, but I suppose you want to deactive the show more link, if the text is only 1 or 2 lines and active it if the text has more than 2 lines.
For this purpose you have to check if the div with the text is bigger than you threshold (2 lines). In my solution I use the height() function which give you the height in pixel. In the original example the link text is not visible if the height is more than 2em.
You better should use also pixel for that or use a tool to convert the units.
Here are my addition lines for a solution with a threshold of 1 line:
var text = $('.text-container');
if (text.height() <= 20) {
$('.show-more').hide();
}
http://jsfiddle.net/JRDzf/
if( $('.text-container').html().indexOf("<br") >= 0 ) {
$(".show-more").hide()
}