admin管理员组文章数量:1405915
I'm not very fluent in javascript, and I feel like this is really basic, but I can't seem to find it online anywhere.
I want to create a link that will trigger a javascript function that makes an image appear in a separate div. It can't be in flash, otherwise I have no objection to the coding language.
I have several images, so I would think that the best way to do this would be to layer them all on top of each other and then increase the z-index each time their link is clicked, but you might have a better idea.
I really just want someway to create a sort of primitive image gallery that doesn't use flash and displays the photo in its div when the corresponding link is clicked.
Thanks in advance, Alex
I'm not very fluent in javascript, and I feel like this is really basic, but I can't seem to find it online anywhere.
I want to create a link that will trigger a javascript function that makes an image appear in a separate div. It can't be in flash, otherwise I have no objection to the coding language.
I have several images, so I would think that the best way to do this would be to layer them all on top of each other and then increase the z-index each time their link is clicked, but you might have a better idea.
I really just want someway to create a sort of primitive image gallery that doesn't use flash and displays the photo in its div when the corresponding link is clicked.
Thanks in advance, Alex
Share Improve this question edited Sep 18, 2012 at 21:52 Codeman 12.4k10 gold badges56 silver badges92 bronze badges asked Sep 18, 2012 at 21:49 user1596790user1596790 311 gold badge1 silver badge3 bronze badges 2- can you create a small, demonstrable example of what you have done so far? – Codeman Commented Sep 18, 2012 at 21:54
- stackoverflow./questions/5451445/… – john-jones Commented Feb 12, 2014 at 9:50
2 Answers
Reset to default 1As 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./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/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./javascript/free_javascript_image_galleries/
I have used this one before, and it's pretty straight forward: http://lokeshdhakar./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.
本文标签: How to make images appear in JavascriptStack Overflow
版权声明:本文标题:How to make images appear in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744940729a2633466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论