How to make images appear in Javascript

那年仲夏 提交于 2019-12-06 10:57:22
Azulflame

As Jessegavin said (found here)

You could make use of the Javascript DOM API. In particular, look at the createElement() method.

You could create a re-usable function that will create an image like so...

function show_image(src, width, height, alt) {
  var img = document.createElement("img");
  img.src = src;
  img.width = width;
  img.height = height;
  img.alt = alt;

  // This next line will just add it to the <body> tag
  document.body.appendChild(img); 
}

Then you could use it like this...

<button onclick="show_image('http://google.com/images/logo.gif', 
  276,110, 'Google Logo");'>Add Google Logo</button> 

There shouldn't be a line break above, I added it so that it could show without scrolling See a working example on jsFiddle: http://jsfiddle.net/Bc6Et/

This should answer your question

There are a ton of javascript libraries, plugins (for jQuery) and other ways of solving this problem.

First thing I would do is to peruse a website like this, to see what's available: http://sixrevisions.com/javascript/free_javascript_image_galleries/

I have used this one before, and it's pretty straight forward: http://lokeshdhakar.com/projects/lightbox2/

In general though you can just do <a href="javascript: showImage()">Link</a> and clicking that will call the javascript function showImage() that you define somewhere. Then layering the divs will work as you have described.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!