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" type="text/css" href="gallery2.css" />
</head>
<body>
	<h1> Snapshots </h1>
	<ul id="imagegallery">
		<li>
			<a href="images/fireworks.jpg" title="A fireworks display"> Fireworks </a>
		</li>
		<li>
			<a href="images/coffee.jpg" title="A cup of coffee"> Coffee </a>
		</li>
		<li>
			<a href="images/rose.gif" title="A rose"> Rose </a>
		</li>
		<li>
			<a href="images/bigben.jpg" title="The famous clock"> Big Ben </a>
		</li>
	</ul>
	<img id="placeholder" src="images/placeholder.jpg" alt="my image gallery" />
	<p id="description"> Choose an image. </p>
	<script type="text/javascript" src="showPic2.js"></script>

	<!-- 
	Some Notes 

	[1] If Javascript support is disabled in consumer's browser, we don't worry about consumer cann't see pictures, because we use a real link in "href" in order to make browser work properly.

	[2] We separate document's structure and document's actions , and much more important thing is we use id "imagegallery" of tag <ul> to make Javascript code be associated with htmldocument's tags.

	-->

</body>
</html>

  

gallery2.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;
}

  

showPic2.js :

window.onload = prepareGallery;
function prepareGallery()
{
	if(!document.getElementsByTagName)
		return false;
	if(!document.getElementById)
		return false;
	if(!document.getElementById("imagegallery"))
		return false;
	var gallery = document.getElementById("imagegallery");
	var links = gallery.getElementsByTagName("a");

	for(var i = 0;i < links.length;i++)
	{
		links[i].onclick = function()
		{
			return showPic(this) ? false : true;
		}
	}
}

function showPic(whichpic)
{
	if(!document.getElementById("placeholder")) return false;
	var source = whichpic.getAttribute("href");
	var placeholder = document.getElementById("placeholder");
	if(placeholder.nodeName != "IMG") return false;
	placeholder.setAttribute("src",source);
	if(document.getElementById("description"))
	{
		var text = whichpic.getAttribute("title") ? whichpic.getAttribute("title") : "";
		var description = document.getElementById("description");
		if(description.childNodes[0].nodeType == 3)
		{
			description.childNodes[0].nodeValue = text;
		}
	}
	return true;
}

  

showPic.js 是关于showPic2.js中代码的解释和一些注意事项:

window.onload = prepareGallery;
function prepareGallery()
{
	if(!document.getElementById) return false;
	if(!document.getElementsByTagName) return false;
	if(!document.getElementById("imagegallery")) return false;
	/*
	We use 3 if statements to check the current browser whether surpports & understands those functions of docunment or not.
	The last if statement is used to check that whether it exists an id "imagegallery" in the associated html file.
	A small tip: if you like putting return statement in a new line, you had better put return statement into a {}.
	*/
	var gallery = document.getElementById("imagegallery");
	var links = gallery.getElementsByTagName("a");

	for(var i = 0; i < links.length;i++)  // traverse all the links of tags <a> in a for loop
	{
		links[i].onclick = function()  // bind every link to a function showPic()
		{
			return showPic(this) ? flase : true;  
			// to solve the problem that it will jump to another web page when you click links if it doesn't return false, and current link will pass to the function showPic() as a parameter
			// and this method of return can solove the problem that you can not open any picture when you delete the "placeholder"
			// if showPic(this) return true(mean that image switches successfully in the showPic()), then prepareGallery() will return false to cancel the default behavior of "onclick"; if showPic() return false, the prepareGallery() will return true to make images can also be opened 
		}
	}
}

function showPic(whichpic)
{
	if(!document.getElementById("placeholder")) return false;
	var source = whichpic.getAttribute("href");
	var placeholder = document.getElementById("placeholder");
	if(!placeholder.nodeName != "IMG") return false;
	// Be attention! nodeName property always return a value of a Capture, even it is a lowercase in html
	placeholder.setAttribute("src",source);

	if(document.getElementById("description"))
	{
		var text = whichpic.getAttribute("title");
		var description = document.getElementById("description");
		if(description.childNodes[0].nodeType == 3)
		{
			description.childNodes[0].nodeValue = text;
		}
	}
	return true;
}

/*
Some Notes:

structed programming : three is a tenet in this theory -- a function can only have one entrance and one exit.
However, in function prepareGallery(), we have three exits! 
In my own opinion, it can be accepted if a function has multiple exits, as long as these exits concentrate in the beginning of a funcition.

onclick  is very smart : we don't worry what will happen if costumer use keyboard to click links, 
because it will trigger the onclick event when you use a tab key to move to a link after pressing the enter key in almost every browser,
so, there is no need for using "onkeypress" event! And "onkeypress" event is not an impeccable function

In this javascript code, I only use four functions of DOM:
[1] getElementById
[2] getElementsByTagName
[3] getAttribute
[4] setAttribute
and we can use HTML-DOM to simplify codes:
document.getElementsByTagName("form") <=> document.forms (HTML-DOM provides object "forms")
element.getAttribute("src") <=> element.src (HTML-DOM provides many propertiees describing html elements: src, href , title and so on)
However, I think DOM Core is easier to use.
*/

  

网页效果:

点击后:

 

 这里完善了图片库中的一些内容。

 

转载于:https://www.cnblogs.com/yojiaku/p/5756384.html

完成一个类似MDN Web文档中的图片画廊页面,首先你需要具备HTML、CSS和JavaScript的基本知识。以下是一个简化的步骤指南: 1. **布局结构**: - 使用HTML5创建一个包含多个`<div>`元素的容器,每个`<div>`作为图片的容器,例如`.image-item`. - 每个`.image-item`可以包含一张图片(`<img>`)标签和描述文字(`<p>`)。 ```html <div class="image-gallery"> <div class="image-item"> <img src="image1.jpg" alt="Image 1 description"> <p>这是图片1的描述</p> </div> <!-- 添加更多图片 --> </div> ``` 2. **样式设计**: - 使用CSS设置整体布局,比如网格布局,图片宽度和间距,以及鼠标悬停效果。 - 可以通过媒体查询调整响应式设计,以便在不同设备上展示良好。 ```css .image-gallery { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; } .image-item:hover { cursor: pointer; } ``` 3. **交互功能**: - JavaScript用于添加动态行为,如图片预览、点击切换图片或弹出详细信息等。 - 你可以使用事件监听器(如`click`事件)来触发相应操作。 ```javascript const imageItems = document.querySelectorAll('.image-item'); imageItems.forEach(item => { item.addEventListener('click', () => { // 展示大图预览或显示详细描述 }); }); ``` 4. **图片数据管理**: - 如果有大量图片,考虑将图片URL和描述存储在一个数组或其他数据结构中,方便管理和更新。 记得在实际项目中,还要处理可能出现的问题,比如图片加载失败、用户交互优化以及SEO优化。如果你需要更详细的代码示例或者特定功能的实现细节,随时提问哦!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值