There are plenty of reasons why you might feel the urge to wax verbose on your website's front page: to prevent your users from having to click through to a new page to find your information, to avoid having to reload the page, or even to improve your front page's SEO. But just because your front page is text-heavy doesn't mean it all needs to be visible at once.Today's tutorial will show you how to hide away extra bits of content using CSS and JavaScript, to be revealed at the click of a button. This is a great technique, because displaying the additional content doesn't require a refresh or navigation to a new page and all your content is still visible to search engine bots that don't pay any attention to CSS or JavaScript.
We'll start with structuring our XHTML appropriately:
show more.
There are three things of importance here: the "show" anchor, the "hide" anchor, and our "hidden" div. Each has been given an ID and a class. The IDs are used by our JavaScript to locate and style the items appropriately. I'm then using the classes to set our "default" CSS. Technically you could just use the IDs the set that CSS as well, but if you wanted more than one hidden section on your page, that could get messy.
You'll notice in the code above that all of our IDs are fairly similar. This is a trick I'm using to simplify our JavaScript, as you'll see later on down the road, so I suggest doing something similar. The class names have no relationship to the JavaScript whatsoever, and could really be whatever you wanted them to be.
Structuring our XHTML
There are plenty of reasons why you might feel the urge to wax verbose on your website's front page: to prevent your users from having to click through to a new page to find your information, to avoid having to reload the page, or even to improve your front page's SEO. But just because your front page is text-heavy doesn't mean it all needs to be visible at once.Today's tutorial will show you how to hide away extra bits of content using CSS and JavaScript, to be revealed at the click of a button. This is a great technique, because displaying the additional content doesn't require a refresh or navigation to a new page and all your content is still visible to search engine bots that don't pay any attention to CSS or JavaScript.
We'll start with structuring our XHTML appropriately:
show more.
There are three things of importance here: the "show" anchor, the "hide" anchor, and our "hidden" div. Each has been given an ID and a class. The IDs are used by our JavaScript to locate and style the items appropriately. I'm then using the classes to set our "default" CSS. Technically you could just use the IDs the set that CSS as well, but if you wanted more than one hidden section on your page, that could get messy.
You'll notice in the code above that all of our IDs are fairly similar. This is a trick I'm using to simplify our JavaScript, as you'll see later on down the road, so I suggest doing something similar. The class names have no relationship to the JavaScript whatsoever, and could really be whatever you wanted them to be.
javascript代码
document.onclick = click;
function click(ev) {
ev = ev || window.event;
var target = ev.target || ev.srcElement;
if(target.className=="show") {
/* 找到兄弟元素 div.show and div.hidden are brothers, their parent is div.content*/
var hid = target.nextSibling;
/* 清除空格回车元素 Clear the space between two div tags. Why could this happen?
* Because we type a space between two div tags for looking indently.*/
if(hid.nodeName=="#text" && /\s/.test(hid.nodeValue)){
hid = hid.nextSibling; /* Get the div#hidtext */
}
if(hid.style.display=='none') {
hid.style.display = "block";
swithcer("block", target);
open(hid);
}
else if(hid.style.display == 'block') {
close(hid);
swithcer("none", target);
}
}
}
var Tween = {
Quad: {
easeOut: function(t,b,c,d) {
return -c*(t/=d)*(t-2) + b;
}
},
Back: {
easeOut: function(t,b,c,d,s){
if (s == undefined) s = 1.70158;
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
}
}
}
function open(hid) {
var t=0, b=0, c=150, d=75;
function run() {
hid.style.height = Math.ceil(Tween.Back.easeOut(t,b,c,d)) + "px";
if(t
/**
* 要执行的算法,返回结果v
*/
public interface Computable<A, V> {
public V comput(final A arg);
}
/**
* 用于缓存数据
*/
public class Memoizer<A, V> implements Computable<A,