`
taro
  • 浏览: 136765 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

javascript学习笔记(1) 缓动效果显示隐藏div

阅读更多

学习javascript,实现两个功能:

 

  1. 显示隐藏div;
  2. 通过Tween算法实现div显示和隐藏的缓动效果。

 

参考链接:缓动效果参考文章:JavaScript html js Tween类型

 

html代码

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="css/hidden.css" media="all" />
		<script src="js/hidden.js" type="text/javascript" ></script>
	</head>
	<body>
		<div id="wrapper">
			<div class="contents">
				<div class="content">
					<h1>Structuring our XHTML</h1>
					<p>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.</p>
					<p>We'll start with structuring our XHTML appropriately:</p>
					<div class="show">show more.</div>
					<div class="hidden" style="display:none">
						<p>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.</p>
						<p>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.</p>
					</div>
				</div>
				<div class="content">
					<h1>Structuring our XHTML</h1>
					<p>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.</p>
					<p>We'll start with structuring our XHTML appropriately:</p>
					<div class="show">show more.</div>
					<div class="hidden" style="display:none">
						<p>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.</p>
						<p>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.</p>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>

 

 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<d) {
			t++;
			setTimeout(run, 10);
		}
	}
	run();
}
function close(hid) {
	var t=0, b=0, c=150, d=75, invald=10;
	function run() {
		hid.style.height = Math.ceil(Math.abs(150 - Tween.Quad.easeOut(t,b,c,d))) + "px";
		if(t<d) {
			t++;
			setTimeout(run, invald);
		}
	}
	run();
	function none() {
		hid.style.display = "none";
	}
	setTimeout(none, invald*(d+30));
}

function swithcer (status, target){
	/* if the hidden area becomes visable. switch the button to "hidden area"*/
	if(status == "block") {
		target.innerHTML = "hidden it.";
		target.style.background = "url(\"img/show_hidden.png\") no-repeat scroll 0 -40px #f0c8a0";
	} 
	else if (status == "none") {
		target.innerHTML = "show more.";
	target.style.background = "url(\"img/show_hidden.png\") no-repeat scroll 0 0 #E4F9F8";
	}
}

 

 css代码

 

p, span {
	margin:0;
}
#wrapper {
	width:960px;
	margin:30px auto;
	padding:20px 0;
	background:#ececec;
}
.contents {
	margin:0 15px;
	padding:5px 10px;
	border:#feb800 dotted 2px;
}
.content p {
	text-indent:20px;
	line-height:120%;
	margin-top:5px;
}
.show {
	width:200px;
	padding-left:25px;
	margin-top:5px;
	font-weight:bold;
	background:#E4F9F8 url(../img/show_hidden.png) 0 0 no-repeat;
	border:#000 dotted 1px;
	cursor:pointer;
}
.hidden {
	display:none; 
	background:#f0c8a0;
	margin:5px 10px;
	padding:0 5px;
	overflow:hidden;
	border:black dotted 1px;
}
分享到:
评论

相关推荐

    javascript学习笔记

    ### JavaScript 学习笔记知识点概览 #### 一、JavaScript 的基本概念与运行方式 - **JavaScript** 是一种脚本语言,主要用于网页的交互性设计,由 Netscape 公司开发。 - **Java Applet** 是由 Sun Microsystems ...

    JavaScript基础学习笔记

    JavaScript 基础学习笔记 本资源摘要信息基于黑马视频记录的学习笔记,涵盖了 JavaScript 基础知识点,包括 HTML、CSS、JavaScript、DOM、事件处理等内容。 HTML 和 CSS 基础 * HTML 结构:head、body、title、...

    javascript学习笔记2

    ### JavaScript 学习笔记2:字符串与事件对象总结 #### 字符串操作方法总结 在JavaScript中,字符串是一系列字符的有序集合。JavaScript提供了一系列内置的方法来帮助我们处理字符串,这些方法可以用来检索、替换...

    韩顺平《轻松搞定网页设计HTML+CSS+JAVASCRIPT》之DIV+CSS学习笔记

    "韩顺平《轻松搞定网页设计HTML+CSS+JAVASCRIPT》之DIV+CSS学习笔记" 本资源主要讲解了 DIV+CSS 的概念和应用, DIV 是 HTML 文档中用来提供结构和背景的元素,而 CSS 是一种用来表现 HTML 或 XML 等文件式样的...

    韩顺平_轻松搞定网页设计html+(DIV+CSS)+javascript视频笔记(全)和memcache笔记

    【网页设计基础】 网页设计是构建互联网应用的基础,它涉及到HTML、CSS和JavaScript等关键技术。...同时,随着Web技术的不断发展,诸如Flexbox、Grid布局、ES6 JavaScript等新工具和技术也在不断涌现,学习永不停步。

    javascript笔记 超级详细 入门

    根据提供的文件信息,我们可以...通过以上知识点的学习,初学者可以建立起对JavaScript的基本认识,并掌握如何在网页中使用JavaScript进行简单的动态效果实现。随着实践的深入,还可以进一步探索更高级的特性和技术。

    Html\CSS+DIV学习笔记(史上最实用的CSS笔记)

    - **行为**:JavaScript添加交互性,如响应用户操作、动画效果等。 3. **静态页面**:XHTML+CSS组合形成的静态页面,意味着内容是预先定义好的,没有服务器端动态生成的内容。 4. **表现与结构分离**:内容(HTML...

    超详细的web前端基础学习笔记

    #### 超详细的web前端基础学习笔记 ##### 前端课程介绍 - **HTML**:HTML (HyperText Markup Language) 是一种标记语言,用于定义网页的结构与内容。它如同网页的骨架,为网页提供基本框架。 - **CSS**:CSS ...

    2011韩顺平html、css+div、javascript课堂笔记全

    在“javascript现场授课笔记”中,你将学习到变量、数据类型、运算符、流程控制(条件语句和循环)、函数等基础语法。同时,JavaScript在DOM(Document Object Model)操作上的应用,如元素的增删改查,事件处理,...

    前端HTML5+CSS+JavaScript学习笔记.docx

    `&lt;script&gt;`标签用于引入JavaScript代码,实现交互性和动态效果。当`src`属性被指定时,它会加载外部脚本文件,而`&lt;noscript&gt;`标签则提供了脚本不支持时的备用内容。 HTML5的标题标签`&lt;h1&gt;`到`&lt;h6&gt;`用于创建层次化的...

    JavaScript 学习笔记

    ### JavaScript 学习笔记 #### 一、JavaScript学习工具 JavaScript 是一种强大的脚本语言,广泛应用于网页开发中。为了更好地学习和运用 JavaScript,选择合适的工具至关重要。 ##### 1.1 编辑器:Visual Studio ...

    html.css.javascript-学习笔记.doc

    HTML、CSS、JavaScript 学习笔记 HTML 篇: 一、HTML 的相关介绍 1. HTML 的介绍 HTML(HyperText Markup Language)是用于创建网页的标记语言。它是一种标准通用标记语言,使用标签来标记网页中的元素,从而...

    jquery 学习笔记

    **jQuery学习笔记** jQuery,作为一个轻量级的JavaScript库,极大地简化了JavaScript的DOM操作、事件处理、动画设计以及Ajax交互。这篇学习笔记将深入探讨jQuery的核心概念和实用技巧,帮助初学者快速上手。 ## 一...

    html+javascript+css学习笔记

    ### HTML+JavaScript+CSS 学习笔记 #### HTML 基础知识点 ##### 1. Meta 标签 Meta 标签主要用于提供网页的元数据信息,这些信息不会直接显示在页面上,但对浏览器、搜索引擎等有重要作用。 - `属性名" content=...

    java项目学习笔记

    ### Java项目学习笔记知识点梳理 #### 一、项目背景与目的 该项目学习笔记记录了从初学者到项目完成过程中的各种技术细节与经验总结。通过实际项目的开发经历,旨在掌握Java编程语言及其相关技术栈的应用。 #### ...

    CSS+DIV学习笔记(史上最实用的CSS笔记).docx

    【CSS+DIV学习笔记概述】 CSS(Cascading Style Sheets)是一种样式表语言,用于描述HTML、XHTML或XML(包括如SVG、MathML等各种XML方言)文档的呈现。CSS描述了如何在媒体(如屏幕、打印机)上展示元素。CSS与HTML...

    jquery和javascript讲解笔记

    通过本文的学习,我们了解了 JavaScript 的基础语法和概念,以及 jQuery 如何简化了这些操作,使得开发者能够更加专注于应用逻辑而非细节。无论是对于初学者还是有经验的开发人员来说,掌握这两种技术都是非常重要的...

    JavaScript DOM编程艺术(中文第二版)学习笔记

    总的来说,JavaScript DOM编程艺术这本书涵盖了JavaScript基础、DOM操作以及实际应用,是学习网页动态交互和JavaScript编程的重要资源。通过学习,开发者能够熟练地创建交互式、动态的网页内容。

Global site tag (gtag.js) - Google Analytics