Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2008-08-09 15:58:14

snthr
Member
Registered: 2007-07-13
Posts: 42

how do i slide blocks of text with javascript?

hi…

i’m hoping to use an effect similar to this demo for a site i’m doing. i want to have an article page with a chunk of text, and a toggle switch which will ‘reveal’ the rest of the text when clicked.

there are, of course, a lot of bits of javascript online that do something along these lines, some better than others.

before i start doing endless google searches for ‘javascript text slide’ and the like, i was hoping some of you might have tried to do something similar to this before.

if so, are there any plugins that give any kind of straightforward way of doing this?

if not, what’s the best way to go about doing it?

thanks.

Offline

#2 2008-08-09 18:49:13

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: how do i slide blocks of text with javascript?

snthr, maybe the easiest way is just to write your own couple lines of Javascript, cause it is how things work :-)

In example just add container div and opening link to your article and then add something, like in example this, written by using jQuery (also comes alongside TXP).

$('.mylink').click(function(){
	$('.container-div').slideToggle('fast');
});

The jquery syntax is quite “anti-javascript” looking :)

Last edited by Gocom (2008-08-09 18:49:46)

Offline

#3 2008-08-09 19:41:39

renobird
Member
From: Gainesville, Florida
Registered: 2005-03-02
Posts: 786
Website

Re: how do i slide blocks of text with javascript?

what’s the best way to go about doing it?

jQuery is the way to go. There are other javascript libraries out there (like MooTools in the example you posted) – but IMO jQuery is the jam.

To expand on Gocom’s example:

In the <head>
<script src="/includes/jquery-1.2.6.js" type="text/javascript"></script>

<script>
//<![CDATA[
$(document).ready(function(){ $('.container-div').hide();
$('.showhide').click(function() { $('.container-div').slideToggle('fast'); return false; }); });
//]]>

</script>

In the <body>
<a href="#" class="showhide">toggle</a>

<div class="container-div">
[Content You Want To Show/Hide in goes here]
</div>

Offline

#4 2008-08-13 06:28:09

andrea
Member
From: la la land
Registered: 2004-02-24
Posts: 62
Website

Re: how do i slide blocks of text with javascript?

thanks! i was looking for something like this.. sweet and simple. ;)

Offline

#5 2008-08-13 18:13:44

snthr
Member
Registered: 2007-07-13
Posts: 42

Re: how do i slide blocks of text with javascript?

that did the trick, thanks…

a secondary question related to this (my javascripting isn’t so hot, hence my confusion)…

how do i change the ‘toggle’ text so that it says “more” when the text is hidden, and then “less” when the text is revealed?

this is possibly very obvious, but i can’t easily see how it’s done.

thanks.

renobird wrote:

jQuery is the way to go. There are other javascript libraries out there (like MooTools in the example you posted) – but IMO jQuery is the jam.

To expand on Gocom’s example:

In the <head>
<script src="/includes/jquery-1.2.6.js" type="text/javascript"></script>

<script>
//<![CDATA[
$(document).ready(function(){ $('.container-div').hide();
$('.showhide').click(function() { $('.container-div').slideToggle('fast'); return false; }); });
//]]>

</script>

In the <body>
<a href="#" class="showhide">toggle</a>

<div class="container-div">
[Content You Want To Show/Hide in goes here]
</div>

Offline

#6 2008-08-13 18:20:03

renobird
Member
From: Gainesville, Florida
Registered: 2005-03-02
Posts: 786
Website

Re: how do i slide blocks of text with javascript?

snthr,

That’s a great question. I’ve seen it done, and I have used a over complicated method that I can’t remember or find right now.
I’ve posted that exact question to the jQuery mailing list – and never really got a good answer. It’s probably pretty easy though.

Anyhow, as a workaround I usually make the link say something like “show/hide details” or something like that.
I’ll be sure to report back here if I find my old solution – or get a good answer from the mailing list.

Offline

#7 2008-08-13 19:49:45

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: how do i slide blocks of text with javascript?

Change
$('.showhide').click(function() { $('.container-div').slideToggle('fast'); return false; }); });

to

$('.showhide').click(function() { 
$('.container-div').slideToggle('fast'); 
if ($('.showhide').html()=="open"){
$('.showhide').html("close");
}else{
$('.showhide').html("open");
}

Although there’s probably a better way. Change ‘open’ and ‘close’ to whatever you’d like but make sure you change ‘open’ in both spots.


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#8 2008-08-13 19:56:09

renobird
Member
From: Gainesville, Florida
Registered: 2005-03-02
Posts: 786
Website

Re: how do i slide blocks of text with javascript?

Matt,

Thanks!

)


T

Offline

#9 2008-08-15 14:25:25

geoff777
Plugin Author
From: Benarrabá Andalucía Spain
Registered: 2008-02-19
Posts: 282
Website

Re: how do i slide blocks of text with javascript?

Anyway to have multiple expand/collapse text boxes on the same page?

Without writing new functions for container-div1 …. container-div2 etc

Thanks Geoff


There are 10 types of people in the world: those who understand binary, and those who don’t.

Offline

#10 2008-08-15 15:59:19

mrdale
Member
From: Walla Walla
Registered: 2004-11-19
Posts: 2,215
Website

Re: how do i slide blocks of text with javascript?

I think you can configure an Accordion to leave multiple items open at once.

Offline

#11 2008-08-15 16:56:34

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: how do i slide blocks of text with javascript?

Maybe you could make each article an accordian


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

#12 2008-08-15 23:07:32

MattD
Plugin Author
From: Monterey, California
Registered: 2008-03-21
Posts: 1,254
Website

Re: how do i slide blocks of text with javascript?

Here’s a cleaner way to handle changing the close button.

<script>
//<![CDATA[
$(document).ready(function(){ $('.container-div').hide();
$('.showhide').click(function() {
 $('.container-div').slideToggle('fast',function(){
$('.showhide').toggle();}); return false; }); }); 
//]]>
</script>

then the close button needs to be duplicated

<a href="" class="showhide">open</a>
<a href="" class="showhide" style="display: none">close</a>

If you are looking to use this for articles in an article list I’m thinking you could include this code in your article form add the article id to the class names.

<script>
//<![CDATA[
$(document).ready(function(){ $('.container-div<txp:article_id />').hide();
$('.showhide<txp:article_id />').click(function() {
                   $('.container-div<txp:article_id />').slideToggle('fast',function(){
                                   $('.showhide<txp:article_id />').toggle();}); return false; }); }); 


//]]>

</script>

<a href="" class="showhide<txp:article_id />">open</a>
<a href="" class="showhide<txp:article_id />" style="display: none">close</a>
<div class="container-div<txp:article_id />">
[Content You Want To Show/Hide in goes here]
</div>

making each article have a unique set of this code. (non-tested idea)


My Plugins

Piwik Dashboard, Google Analytics Dashboard, Minibar, Article Image Colorpicker, Admin Datepicker, Admin Google Map, Admin Colorpicker

Offline

Board footer

Powered by FluxBB