• gallery3


    这个版本的gallery更加的将页面和行为分离,通过DOM的插入节点功能

    将只用来占位的img标签和description标签(<p id="description">Choose a image</p><img id="palceholder" src="images/e.jpg" alt="hehe">

    )通过js来插入到页面。

    方法insertAfter是向节点添加节点

    function insertAfter(newElement,targetElement){
    		var parent=targetElement.parentNode;
    		if(parent.lastChild==targetElement){
    			parent.appendChild(newElement);
    		}else{
    			parent.insertBefore(newElement,targetElement.nextSibling);
    		}
    }
    

    方法addLoadEvent是为页面添加自动加载事件

    function addLoadEvent(func){
    	var oldonload=window.onload;
    	if(typeof window.onload!='function'){
    		window.onload=func;
    	}else{
    		window.onload=function(){
    			oldonload();
    			func();
    		}
    	}
    }
    

      

    这两个方法是非常实用的,以后将会经常用到。

    以下是代码部分

    HTML

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>Gallery</title>
    	<link rel="stylesheet" href="css/reset.css">
    	<link rel="stylesheet" href="css/style.css">
    </head>
    <body>
    	<h1>Gallery</h1>
    	<ul id="imagegallery">
    		<li><a href="images/a.jpg" title="Lorem ipsum dolor sit amet,"><img src="images/a.jpg" alt="Lorem"></a></li>
    
    		<li><a href="images/b.jpg" title="consectetur adipisicing"><img src="images/b.jpg" alt="Lorem"></a></li>
    		<li><a href="images/c.jpg" title="alias ab quos cupiditate"><img src="images/c.jpg" alt="Lorem"></a></li>
    		<li><a href="images/d.jpg" title="dignissimos eligendi cum "><img src="images/d.jpg" alt="Lorem"></a></li>
    	</ul>
    	
    	<!-- <p id="description">Choose a image</p>
    	
    	<img id="palceholder" src="images/e.jpg" alt="hehe"> -->
    	
    	<script src="js/showPic.js"></script>
    </body>
    </html>
    

      

    JavaScript:

    function addLoadEvent(func){
    	var oldonload=window.onload;
    	if(typeof window.onload!='function'){
    		window.onload=func;
    	}else{
    		window.onload=function(){
    			oldonload();
    			func();
    		}
    	}
    }
    
    
    function insertAfter(newElement,targetElement){
    		var parent=targetElement.parentNode;
    		if(parent.lastChild==targetElement){
    			parent.appendChild(newElement);
    		}else{
    			parent.insertBefore(newElement,targetElement.nextSibling);
    		}
    }
    
    function preparePlaceholder(){
    		if(!document.createElement)return false;
    		if(!document.createTextNode)return false;
    		if(!document.getElementById)return false;
    		if(!document.getElementById("imagegallery"))return false;
    		
    		var palceholder=document.createElement("img");
    		palceholder.setAttribute("id","palceholder");
    		palceholder.setAttribute("src","images/e.jpg");
    		palceholder.setAttribute("alt","my image gallery");
    
    		var description=document.createElement("p");
    		description.setAttribute("id","description");
    		var desctext=document.createTextNode("Choose an image");
    
    		description.appendChild(desctext);
    		var gallery=document.getElementById("imagegallery");
    		insertAfter(description,gallery);
    		insertAfter(palceholder,description);
    }
    
    function prepareGallery(){
    	//判断是否支持以下这些方法
    	if(!document.getElementById)return false;
    	if(!document.getElementsByTagName)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);
    		}
    		links[i].onkeypress=links[i].onclick;
    	}
    }
    
    function showpic(whichpic){
    	if(!document.getElementById("palceholder"))return true;
    	var source=whichpic.getAttribute("href");
    	var palceholder=document.getElementById("palceholder");
    	palceholder.setAttribute("src",source);
    
    	if(document.getElementById("description")){
    		if(whichpic.getAttribute("title")){
    			var text=whichpic.getAttribute("title");
    		}else{
    			text="";
    		}
    		var description= document.getElementById("description");
    		if(description.firstChild.nodeType===3)
    		description.firstChild.nodeValue=text;
    	}
    	return false;
    }
    
    addLoadEvent(preparePlaceholder);
    addLoadEvent(prepareGallery);
    

      

  • 相关阅读:
    ClickOne :添加ClickOnce程序的快捷方式
    C#: 通过html调用WinForm 。。。。。
    JQuery:常用方法一览
    Jquery:Ajax解析XML数据(同步及异步调用)
    系统管理:服务器超级隐藏账户的建立与检测大全[转载]
    Jquery :动态给Table指定行或尾行插入tr和相关,遍历页面所有控件获取id
    .Net : webBrowser弹出新页面,在当前Form窗体打开,可设置屏蔽当前网页的js错误。。。。
    Visual Studio 设置:包括根据不同类型的开发活动对集成开发环境 (IDE) 所做的各种自定义设置
    VB.Net : ApplicationEvents类中,创建主程序的快捷方式 , 捕获程序未Try的错误及抛出相关的类和方法名 , 是否联机等相关。。
    [转载]JavaScript 图片预览效果
  • 原文地址:https://www.cnblogs.com/Eyrum/p/4572509.html
Copyright © 2020-2023  润新知