单击缩略图以更改主图像? [英] Click Thumbnail to Change Main Image?

查看:98
本文介绍了单击缩略图以更改主图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在学习JS大约一个月并完成了大约4门课程后,我仍然无法解决如何单击缩略图时如何更改图像!我想做的很简单,我只想在单击缩略图时更改主图像!在此示例中,div中有两个缩略图图像,而上方有一个主图像.我只想在单击缩略图时更改主图像.我知道这是DOM操作,并且认为是:document.getElementById .....?

After learning JS for about a month now and completing around 4 courses I am still unable to work out how to change an image when clicking a thumbnail! What I want to do is simple, I just want to change the Main Image when a thumbnail is clicked! In this example there are two thumbnail images in a div and a main image above them. I just want to change the main image when a thumbnail is clicked. I know this is DOM Manipulation and think it is: document.getElementById.....?

我做了一个小页面,以便我可以学习/尝试不同的事情,最后放弃并寻求帮助!代码如下:

I have make a small page so that I can learn / try different things and and finally giving up and asking for help! The code is as follows:

#MainContainer {
position: relative;
margin:0px auto;
width: 500px;
height: 400px;
border: 1px solid black;
}
#MainImage {
position: absolute;
top: 10px;
left: 50px;
width: 398px;
height: 265px;
background: url(MainImage01.jpg);
border: 1px solid black;
}
#TNBodyContainer {
position: absolute;
top: 290px;
left: 100px;
border: 1px solid black;
width: 268px;
height: 88px;
}
#TNOne {
position: relative;
width: 133px;
height: 88px;
background: url(SmallImage01.jpg);
}
#TNTwo {
position: relative;
left:135px;
width: 133px;
height: 88px;
background: url(SmallImage02.jpg);
}

<body>
<div id="MainContainer">
    <div id="MainImage"></div>
    <div id="TNBodyContainer">
        <div id="TNOne">
            <div id="TNTwo"></div>
        </div>
    </div>

非常感谢您的帮助.

马盖特

推荐答案

单击任意一个缩略图时,您需要添加一些脚本来更改图像.加载页面时调用此函数.更改图像名称以适合. 这应该放在html页面的部分中.

You need to add some scripting to change the image when either of the thumbnails are clicked. This function is called when the page is loaded. Change the image names to suit. This should be placed in the section of the html page.

<script>
window.onload = function() {
        var mainImg = document.getElementById('Main');

        document.getElementById('TNOne').onclick = function() {
                mainImg.src = 'main1.jpg';
                //alert('one clicked');
        };
        document.getElementById('TNTwo').onclick = function() {
                mainImg.src = 'main2.jpg';
                //alert('two clicked');
        };
};
</script>

两个缩略图div成为具有相同ID的<img>标签. 类似地,也定义了主<img>(其id ="Main").现在的元素 是可以点击的.

The two thumbnail divs become <img> tags with the same IDs. Similarly the main <img> is defined also (with id="Main"). Now the elements are clickable.

<div id="MainContainer">
    <div id="MainImage">
        <img id="Main" src="MainImage01.jpg"</img>
    </div>
    <div id="TNBodyContainer">
        <img id="TNOne" src="thumb1.jpg"></img>
        <img id="TNTwo" src="thumb2.jpg"></img>
    </div>
</div>

最后是缩略图的CSS,这里的float用于将缩略图保留在TNBodyContainer div中的同一行中.

Finally CSS for the thumbnails, here float is used to keep the thumbnails in the same line within the TNBodyContainer div.

TNOne {
width: 133px;
height: 88px;
float:left;
}
#TNTwo {
width: 133px;
height: 88px;
float:left;
}

这篇关于单击缩略图以更改主图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆