Javascript Images Gallery

文件目录:

gallery.html

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>
	<title>Image Gallery</title>
	<style type="text/css">
		body{
			font-family: "Helvetica","Arial","serif";
			color: #333;
			background-color: #ccc;
			margin: 1em 10%;
		}
		h1{
			color:#333;
			background-color: transparent;
		}
		a{
			color: #c60;
			background-color: transparent;
			font-weight: bold;
			text-decoration: none;
			cursor: pointer;
		}
		ul{
			padding: 0;
		}
		li{
			float: left;
			padding: 1em;
			list-style: none;
		}
		img{
			display: block;
			clear: both;
		}
	</style>
</head>
<body>
	<h1> Snapshots </h1>
	<ul>
		<!-- if we want to use showPic(), we need to add an event handler to <a> ;
				to avoid jumping to another page when we click links, we should add return false to onclick event handler.
			However, when I write ronclick="showPic(this);return false;", the Chrome does not work, so I change the attribute "href" to "alt" to avoid this problem. <=== this is a wrong solvement               When I tried many times, I find the solvement: I forget to add "return true" at the end of function showPic(). <=== this is a correct solvement          -->
		<li>
			<a href="images/fireworks.jpg" title="A fireworks display" onclick="showPic(this); return false"> Fireworks </a>
		</li>
		<li>
			<a href="images/coffee.jpg" title="A cup of black coffee" onclick="showPic(this); return false"> Coffee </a>
		</li>
		<li>
			<a href="images/rose.gif" title="A rose" onclick="showPic(this); return false"> Rose </a>
		</li>
		<li>
			<a href="images/bigben.jpg" title="The famous clock" onclick="showPic(this); return false"> Big Ben </a>
		</li>
	</ul>
	<img id="placeholder" src="images/placeholder.jpg" alt="my image gallery" />
	<p id="description"> Choose an image. </p>
	<!-- when we choose a picture, the content of <p> will also change. -->
	<script src="showPic.js"></script>
</body>
</html>

  showPic.js

/* to change placeholder.jpg into what picture we want */
		function showPic(whichpic) {
			if(!document.getElementById) return false;
			var source = whichpic.getAttribute("href");
			var placeholder = document.getElementById("placeholder");
			placeholder.setAttribute("src",source);

			//change the content of <p>
			var text = whichpic.getAttribute("title");
			var description = document.getElementById("description");
			/* if we use "description.nodeValue", what we obtain is not the content of <p>,
			but null(nodeValue attribute of the element itself is an empty value),
			because the content of <p> is another child node, it is the first child node of <p>.
			We should use follows to obtain it.*/
			console.log(description.childNodes[0].nodeValue);
			// this sentence equal to "console.log(description.firstchild.nodeValue);", however, in fact, Chrome can only read childNodes[0], not firstchild
			description.childNodes[0].nodeValue = text;
			console.log(description.childNodes[0].nodeValue);
			/* The value of the nodeValue properties of object description‘s first child node is set to the value of the variable text. */              return true;
		}
		/* whichpic stands an element node, specifically, it is a <a> pointed a picture.
		We need fracture the path of picture and this can be made by transfer attribute "href" to getAttribute().
		Then we should save this path in var source.
		Next, we also need to obtain placeholder.jpg, to aviod repeating the same operation, put this element to a viriable "placeholder".
		Last, use setAttribute() to finish this function showPic().
		*/

		// follow function just to show nodeType how to work
		function countBodyChild()
		{
			var body_element = document.getElementsByTagName("body")[0];
			console.log(body_element.childNodes.length);
			console.log(body_element.nodeType);
		}
		/* We can find that the number of body‘s element is far more than we expected.
			Because childNodes will return all types nodes, not only element nodes.
			However, every node has a nodeType attribute,
			this can let us know what type the nodes are.
			If it is element node, nodeType returns 1; attibute node returns 2; text node returns 3.
		*/

		window.onload = countBodyChild;

  所有的代码解释都在代码里头了!o(* ̄▽ ̄*)o

下面是效果图:最开始的页面

点击时:

时间: 2024-10-24 02:32:15

Javascript Images Gallery的相关文章

A JavaScript Image Gallery

childNodes property: The childNodes property is a way of getting information about the children of any element in a document's node tree. It returns an array containing all the children of an element node : element.childNodes; Lets say you wanted to

Javascript Image Gallery 修改

文件目录: 其中,所需的只有 showPic2.js & gallery2.html & gallery2.css & images gallery2.html : <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Image Gallery</title> <link rel="stylesheet&q

35 Gallery – Ajax Slide

http://html5up.net/overflow (PC,Mobile,Table) http://www.iloveyouwp.com/35-scripts-for-galleries-slideshows-and-lightboxes.htmlhttp://ajaxphotogallery.com/ajax-image-galleries.htmlhttp://coffeescript.org/http://www.lokeshdhakar.com/projects/lightbox2

Free Slideshow, Gallery And Lightboxes Scripts

http://www.noupe.com/design/free-slideshow-gallery-and-lightboxes-scripts.html October 14th, 2009 by Obaid ur Rehman Free Slideshows And Lightboxes Scripts Polaroid Gallery v.1.01Polaroid Gallery is a free open-source Flash gallery developed by Chris

Learning JavaScript Design Patterns -- A book by Addy Osmani

Learning JavaScript Design Patterns A book by Addy Osmani Volume 1.6.2 Tweet Copyright © Addy Osmani 2015. Learning JavaScript Design Patterns is released under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 unported license. It

javascript图片库

将图片发布到网上的办法很多,可以简单地把所有图片都放在一个网页中,但是会导致这个网页过于庞大.为每张图片分别创建一个网页的解决办法值得考虑,但是制作过程需要花费非常多的时间和精力. 如果想要两全其美,那我有一种方法,利用JavaScript制作一个图片库,把整个图片库的浏览链接到图片库主页,用户点击某个链接时,那张图片才会且在本页面显示. html文件的编写 <html xmlns="http://www.w3.org/1999/xhtml"><head><

平稳退化,JS和HTML标记分离,极致性能的JavaScript图片库

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>JavaScript图片库</title> 6 <script type="text/javascript"> 7 <!--平稳退化,JS和HTML标记分离,极致性能的JS图片库--> 8 fu

JavaScript模块加载框架sea.js 学习一

简单总结sea.js 学习 文件目录结构 /sea/sea.js      下载地址  http://seajs.org/docs/#downloads /sea/jquery-sea.js   下载地址 http://jquery.com/download/ /sea/sea_config.js /sea/home.jsdata.js /sea/data.js 1.html页面代码文件 <style> .ch{height:200px;width:200px;background:#ccc;

Javascript 动态添加节点(thinking in DOM)

test.html & example.js 文件说明了传统技术:document.write & innerHTML 的用法 test.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title> Test </title> </head> <body> <div id="testd