javascript照片切换特效代码(如何用javascript实现三张以上的图片在点击图片时切换到下一张,我是初学者,希望用数组和循环实现求代码)

本文目录
- 如何用javascript实现三张以上的图片在点击图片时切换到下一张,我是初学者,希望用数组和循环实现求代码
- JavaScript 的网页图片切换代码
- 在html里面怎么制作多张图片翻页效果,下面有文字一点击就进去指定的页面
- JS特效,一个DIV,里面有四张图,两边有两个按钮用来控制四张图的切换
如何用javascript实现三张以上的图片在点击图片时切换到下一张,我是初学者,希望用数组和循环实现求代码
《script type="text/javascript"》
window.onload = function() {
// 图片地址数组
var image_urls = ;
// 初始化数组键值 (0 = 第一个图片)
var idx = 0;
// 点击 id 为 img 的图片
document.getElementById("img").onclick = function() {
// idx + 1,如果为最后一张,还原回第一张
idx = idx === image_urls.length - 1 ? 0 : idx + 1;
// 设置 src
this.src = image_urls;
// 测试
alert(this.src);
};
};
《/script》
《img id="img" src="first.jpg" /》
上面代码只用了一个 img,点击更换地址。
还是你想全部隐藏(除当前图片),点击后显示下一张?
JavaScript 的网页图片切换代码
***隐藏网址***
***隐藏网址***
《head》
***隐藏网址***
《title》网页特效 首页图片切换《/title》
《style type="text/css"》
/* Reset style */
* { margin:0; padding:0; word-break:break-all; }
body {
background:#fff;
color:#000000;
font:12px/1.6em Helvetica, Arial, sans-serif;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
h1, h3, h3, h4, h5, h6 { font-size:1em; }
a { color:#0287CA; text-decoration:none; }
a:hover { text-decoration:underline; }
ul, li { list-style:none; }
fieldset, img { border:none; }
legend { display:none; }
em, strong, cite, th { font-style:normal; font-weight:normal; }
input, textarea, select, button { font:12px Helvetica, Arial, sans-serif; }
table { border-collapse:collapse; }
html { overflow:-moz-scrollbars-vertical; } /*Always show Firefox scrollbar*/
/* iFocus style */
#ifocus { width:650px; height:245px; margin:0px; border:1px solid #DEDEDE; background:#F8F8F8; }
#ifocus_pic { display:inline; position:relative; float:left; width:540px; height:225px; overflow:hidden; margin:10px 0 0 10px; }
#ifocus_piclist { position:absolute; }
#ifocus_piclist li { width:550px; height:225px; overflow:hidden; }
#ifocus_piclist img { width:550px; height:225px; }
#ifocus_btn { display:inline; float:right; width:91px; margin:9px 9px 0 0; }
#ifocus_btn li { width:91px; height:57px; cursor:pointer; opacity:0.5; -moz-opacity:0.5; filter:alpha(opacity=50); }
#ifocus_btn img { width:75px; height:45px; margin:7px 0 0 11px; }
#ifocus_btn .current { background: url(img/ifocus_btn_bg.gif) no-repeat; opacity:1; -moz-opacity:1; filter:alpha(opacity=100); }
#ifocus_opdiv { position:absolute; left:0; bottom:0; width:545px; height:35px; background:#000; opacity:0.5; -moz-opacity:0.5; filter:alpha(opacity=50); }
#ifocus_tx { position:absolute; left:8px; bottom:8px; color:#FFF; }
#ifocus_tx .normal { display:none; }
《/style》
《script type="text/javascript"》
function $(id) { return document.getElementById(id); }
function addLoadEvent(func){
var oldonload = window.onload;
if (typeof window.onload != ’function’) {
window.onload = func;
} else {
window.onload = function(){
oldonload();
func();
}
}
}
function moveElement(elementID,final_x,final_y,interval) {
if (!document.getElementById) return false;
if (!document.getElementById(elementID)) return false;
var elem = document.getElementById(elementID);
if (elem.movement) {
clearTimeout(elem.movement);
}
if (!elem.style.left) {
elem.style.left = "0px";
}
if (!elem.style.top) {
elem.style.top = "0px";
}
var xpos = parseInt(elem.style.left);
var ypos = parseInt(elem.style.top);
if (xpos == final_x && ypos == final_y) {
return true;
}
if (xpos 《 final_x) {
var dist = Math.ceil((final_x - xpos)/10);
xpos = xpos + dist;
}
if (xpos 》 final_x) {
var dist = Math.ceil((xpos - final_x)/10);
xpos = xpos - dist;
}
if (ypos 《 final_y) {
var dist = Math.ceil((final_y - ypos)/10);
ypos = ypos + dist;
}
if (ypos 》 final_y) {
var dist = Math.ceil((ypos - final_y)/10);
ypos = ypos - dist;
}
elem.style.left = xpos + "px";
elem.style.top = ypos + "px";
var repeat = "moveElement(’"+elementID+"’,"+final_x+","+final_y+","+interval+")";
elem.movement = setTimeout(repeat,interval);
}
function classNormal(iFocusBtnID,iFocusTxID){
var iFocusBtns= $(iFocusBtnID).getElementsByTagName(’li’);
var iFocusTxs = $(iFocusTxID).getElementsByTagName(’li’);
for(var i=0; i《iFocusBtns.length; i++) {
iFocusBtns.className=’normal’;
iFocusTxs.className=’normal’;
}
}
function classCurrent(iFocusBtnID,iFocusTxID,n){
var iFocusBtns= $(iFocusBtnID).getElementsByTagName(’li’);
var iFocusTxs = $(iFocusTxID).getElementsByTagName(’li’);
iFocusBtns.className=’current’;
iFocusTxs.className=’current’;
}
function iFocusChange() {
if(!$(’ifocus’)) return false;
$(’ifocus’).onmouseover = function(){atuokey = true};
$(’ifocus’).onmouseout = function(){atuokey = false};
var iFocusBtns = $(’ifocus_btn’).getElementsByTagName(’li’);
var listLength = iFocusBtns.length;
iFocusBtns.onmouseover = function() {
moveElement(’ifocus_piclist’,0,0,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,0);
}
if (listLength》=2) {
iFocusBtns.onmouseover = function() {
moveElement(’ifocus_piclist’,0,-225,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,1);
}
}
if (listLength》=3) {
iFocusBtns.onmouseover = function() {
moveElement(’ifocus_piclist’,0,-450,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,2);
}
}
if (listLength》=4) {
iFocusBtns.onmouseover = function() {
moveElement(’ifocus_piclist’,0,-675,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,3);
}
}
}
setInterval(’autoiFocus()’,3500);
var atuokey = false;
function autoiFocus() {
if(!$(’ifocus’)) return false;
if(atuokey) return false;
var focusBtnList = $(’ifocus_btn’).getElementsByTagName(’li’);
var listLength = focusBtnList.length;
for(var i=0; i《listLength; i++) {
if (focusBtnList.className == ’current’) var currentNum = i;
}
if (currentNum==0&&listLength!=1 ){
moveElement(’ifocus_piclist’,0,-225,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,1);
}
if (currentNum==1&&listLength!=2 ){
moveElement(’ifocus_piclist’,0,-450,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,2);
}
if (currentNum==2&&listLength!=3 ){
moveElement(’ifocus_piclist’,0,-675,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,3);
}
if (currentNum==3 ){
moveElement(’ifocus_piclist’,0,0,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,0);
}
if (currentNum==1&&listLength==2 ){
moveElement(’ifocus_piclist’,0,0,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,0);
}
if (currentNum==2&&listLength==3 ){
moveElement(’ifocus_piclist’,0,0,5);
classNormal(’ifocus_btn’,’ifocus_tx’);
classCurrent(’ifocus_btn’,’ifocus_tx’,0);
}
}
addLoadEvent(iFocusChange);
《/script》
《/head》
《body》
《br /》
《div align="center"》
《div id="ifocus"》
《div id="ifocus_pic"》
《div id="ifocus_piclist" style="left:0; top:0;"》
《ul》
《li》《a href="#" target="_blank"》《img src="/edu/img/js/200909/1.jpg" alt="武林三国" border="0" /》《/a》《/li》
《li》《a href="#" target="_blank"》《img src="/edu/img/js/200909/2.jpg" alt="武林英雄" border="0" /》《/a》《/li》
《li》《a href="#" target="_blank"》《img src="/edu/img/js/200909/3.jpg" alt="商业大亨" border="0" /》《/a》《/li》
《li》《a href="#" target="_blank"》《img src="/edu/img/js/200909/4.jpg" alt="帝国远征" border="0" /》《/a》《/li》
《/ul》
《/div》
《div id="ifocus_opdiv"》《/div》
《div id="ifocus_tx"》
《ul》
《li class="current"》2008年度排名第一的网页游戏《/li》
《li class="normal"》2009年最新的网页游戏 《/li》
《li class="normal"》商业大亨,挑战亿万富翁《/li》
《li class="normal"》一款2009年不得不玩的帝国远征《/li》
《/ul》
《/div》
《/div》
《div id="ifocus_btn"》
《ul》
《li class="current"》《img src="/edu/img/js/200909/s1.jpg" alt="" /》《/li》
《li class="normal"》《img src="/edu/img/js/200909/s2.jpg" alt="" /》《/li》
《li class="normal"》《img src="/edu/img/js/200909/s3.jpg" alt="" /》《/li》
《li class="normal"》《img src="/edu/img/js/200909/s4.jpg" alt="" /》《/li》
《/ul》
《/div》
《/div》
《/div》
《/body》
《/html》
自己改图片链接和大小~~
在html里面怎么制作多张图片翻页效果,下面有文字一点击就进去指定的页面
用JS脚本制作简单快捷,只需少量代码如下:
《html》
《head》
***隐藏网址***
***隐藏网址***
《title》图片左右翻页《/title》
《style》
《!--
body{
text-align:center;
margin:20px 0px 0px 0px;
}
table{
border:0px;
}
.sp{
width:520px;
height:400px;
border:2px solid #FFCC00;
text-align:center;
line-height:400px;
}
.sn{
width:520px;
height:30px;
text-align:center;
line-height:30px;
}
img{
border:0px;
cursor:pointer;
}
--》
《/style》
《script language="javascript"》
《!--
var photo=new Array(4);
var photoname=new Array(4);
***隐藏网址***
photoname="柳梦璃"
***隐藏网址***
photoname="韩菱纱"
***隐藏网址***
photoname="云天河"
***隐藏网址***
photoname="慕容紫英"
var currid=0;
function showphoto(){
if(window.event.x》window.screen.width/2){
currid=currid+1;
}else{
currid=currid-1;
}
if(currid》=4){
currid=0;
}else if(currid《0){
currid=3;
}
inphoto.src=photo;
fn.innerHTML=photoname;
}
function changestyle(o){
if(window.event.offsetX》=o.clientWidth/2){
***隐藏网址***
o.alt="点击显示下一张";
}else{
***隐藏网址***
o.alt="点击显示上一张";
}
}
function preloadimage(){
photo1=new Image();
***隐藏网址***
photo2=new Image();
***隐藏网址***
photo3=new Image();
***隐藏网址***
photo4=new Image();
***隐藏网址***
photopre=new Image();
***隐藏网址***
photonext=new Image();
***隐藏网址***
}
--》
《/script》
《/head》
《body 》
《table》
《tr》
***隐藏网址***
《/tr》
《tr》
《td class="sn"》《span id="fn"》柳梦璃《/span》《/td》
《/tr》
《tr》
《td class="sn"》点击图片左右显示上下张《/td》
《/tr》
《/table》
《/body》
《/html》
JS特效,一个DIV,里面有四张图,两边有两个按钮用来控制四张图的切换
换四张图跟换一张图差不多
《HTML》
《HEAD》
《TITLE》 test 《/TITLE》
《/HEAD》
《body》
《img id="test" src="1.jpg"/》《input type="button" value="点击换图" onclick="change()"/》
《SCRIPT type="text/javascript"》
function change(){
var test_img=document.getElementById("test").getAttribute("src");
var reg=/.*1\.jpg/;
if(reg.test(test_img))
document.getElementById("test").setAttribute("src","2.jpg");
else{
document.getElementById("test").setAttribute("src","1.jpg");
}
}
《/SCRIPT》
《/body》
《/HTML》

本文相关文章:
javascript焦点图(win7 中ie8不能显示javascript焦点图 ,请问为什么)
2026年5月20日 23:40
scrollthrough意思(“scroll”是什么意思)
2026年9月7日 08:00
更多文章:
全球新冠肺炎疫情背景下航运发展(盐田港复苏日志:半年历劫从“低谷”到“爆仓” 疫情之后巨轮如何越洋航行)
2026年9月7日 17:10
matlab求解带字母参数方程组(我想matlab求一个关于x,y的方程组 ab c d f e h m n 都是参数)
2026年9月7日 16:30
oracle中的循环语句(下面哪个不是oracle程序设计中的循环语句 a for)
2026年9月7日 15:30
电脑里2个系统怎么删除一个(电脑开机显示有两个系统,如何删除一个)
2026年9月7日 12:20
scrollthrough意思(“scroll”是什么意思)
2026年9月7日 08:00
怎么激活keygen(注册机如何激活cad2008一个简单激活cad2008的方法)
2026年9月7日 06:30
vba编写excel插件(excel vba中能否动态创建控件)
2026年9月7日 04:40



