cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <h2>图片库</h2>
<ul id="imagegallery">
  <li><a href="http://oqef2is9h.bkt.clouddn.com/1.jpg" title="first pic">图片一</a></li>
  <li><a href="http://oqef2is9h.bkt.clouddn.com/2.jpg" title="second pic">图片二</a></li>
  <li><a href="http://oqef2is9h.bkt.clouddn.com/3.jpg" title="third pic">图片三</a></li>
  <li><a href="http://oqef2is9h.bkt.clouddn.com/6.jpg" title="fourthly pic">图片四</a></li>
</ul>

            
          
!
            
              body {
  color: #444;
  font-family: "微软雅黑";
}
ul li {
  display: inline-block;
  list-style: none;
  margin-right:20px;
}
a,a:hover {
  color: #444;
  text-decoration: none;
}

	a img {border:0;width:100px;}
            
          
!
            
              //js中创建p标签和img标签,并把他们插入到模板元素的后面
function preparePlaceholder(){   
	//加上就不对,原因还未找出
//	if(!document.createElement) return false;
//	if(!document.createTextNode) return false;
//	if(!document.getElementById) return false;
//	if(!document.getElementById("imagegallery")) return false;
	
	var img = document.createElement("img");
	img.setAttribute("id","placeholder");
	img.setAttribute("src","http://oqef2is9h.bkt.clouddn.com/1K.png");
	img.setAttribute("alt","default pic");
	
	var p = document.createElement("p");
	p.setAttribute("id","description");
	var ptext = document.createTextNode("This is a description");
	p.appendChild(ptext);
	
	var imagegallery=document.getElementById("imagegallery");
	insertAfter(img,imagegallery);
	insertAfter(p,img);
}

//获取a的href传给img的src;获取a的title传给p的text
function showPic(whichpic){
	//if(!document.getElementById("placeholder")) return false;
	var placeholder=document.getElementById("placeholder");
	var href=whichpic.getAttribute("href");
	//if(placeholder.nodeName != "IMG") return false;
	placeholder.setAttribute("src",href);
	
	if(whichpic.getAttribute("title")){
		var title=whichpic.getAttribute("title");
	}else{
		var title="";
	}
	var description=document.getElementById("description");
	if(description.firstChild.nodeType==3){
		description.firstChild.nodeValue=title;
	}
	
	return false;
}

//给a添加点击事件
function prepareGallery(){
	//if(!document.getElementsByTagName || !document.getElementById) return false;
	//if(!document.getElementById("imagegallery")) return false;
	
	var imagegallery=document.getElementById("imagegallery");
	var links=imagegallery.getElementsByTagName("a");
	for(var i=0;i<links.length;i++){
		links[i].onclick=function(){
			return showPic(this);
		}
	}
}

//在现有元素后插入一个新元素
function insertAfter(newElement,targetElement){
	var parent = targetElement.parentNode;
	if(parent.lastChild == targetElement){  //如果目标元素是parent的最后一个子元素,那就把新元素插入到追加到parent上,这样新元素就恰好被插入到目标元素后面
		parent.appendChild(newElement);
	}else{
		parent.insertBefore(newElement,targetElement.nextSibling);  //如果不是,那就把新元素插入到目标元素的下一个兄弟元素的前面
	}
}

//页面加载函数
function addLoadEvent(func){
	var oldonload=window.onload;
	if(typeof window.onload !="function"){
		window.onload=func;
	}else{
		window.onload=function(){
			oldonload();
			func();
		}
	}
}

addLoadEvent(prepareGallery);
addLoadEvent(preparePlaceholder);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console