当点击图片href =#时,它会滚动到顶部

时间:2012-07-24 16:45:35

标签: javascript html

以下是我用于我网站的代码 每个图像链接都设置为在单击时前进到下一个图像 问题是,当我点击图像时,页面会滚动回到顶部。我想要停止

我在onclick标签上使用了return false标签,但是当单击图像时,它会不断滚动图像数组。

任何解决方案?以下是我的代码

<script language="JavaScript">

  <!-- -->


var interval = 1500;
var random_display = 0;
var imageDir = "imgs/";

var imageNum = 0;
imageArray = new Array();
imageArray[imageNum++] = new imageItem(imageDir + "1.jpg");
imageArray[imageNum++] = new imageItem(imageDir + "2.jpg");
imageArray[imageNum++] = new imageItem(imageDir + "3.jpg");
imageArray[imageNum++] = new imageItem(imageDir + "4.jpg");
imageArray[imageNum++] = new imageItem(imageDir + "5.jpg");
imageArray[imageNum++] = new imageItem(imageDir + "6.jpg");
imageArray[imageNum++] = new imageItem(imageDir + "7.jpg");
imageArray[imageNum++] = new imageItem(imageDir + "8.jpg");
imageArray[imageNum++] = new imageItem(imageDir + "9.jpg");
imageArray[imageNum++] = new imageItem(imageDir + "10.jpg");

var imageNumTwo = 0;
imageArrayTwo = new Array();
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t1.jpg");
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t2.jpg");
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t3.jpg");
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t4.jpg");
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t5.jpg");
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t6.jpg");
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t7.jpg");
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t8.jpg");
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t9.jpg");
imageArrayTwo[imageNumTwo++] = new imageItem(imageDir + "t10.jpg"); 

var imageNumThree = 0;
imageArrayThree = new Array();
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th1.jpg");
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th2.jpg");
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th3.jpg");
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th4.jpg");
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th5.jpg");
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th6.jpg");
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th7.jpg");
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th8.jpg");
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th9.jpg");
imageArrayThree[imageNumThree++] = new imageItem(imageDir + "th10.jpg");    


var totalImages = imageArray.length;
var totalImagesTwo = imageArrayTwo.length;
var totalImagesThree = imageArrayThree.length;

function imageItem(image_location) {
    this.image_item = new Image();
    this.image_item.src = image_location;
}
function get_ImageItemLocation(imageObj) {
    return(imageObj.image_item.src)
}


function randNum(x, y) {
    var range = y - x + 1;
    return Math.floor(Math.random() * range) + x;
}

function getNextImage() {
    if (random_display) {
        imageNum = randNum(0, totalImages-1);
    }
    else {
        imageNum = (imageNum+1) % totalImages;
    }   

    var new_image = get_ImageItemLocation(imageArray[imageNum]);
return(new_image);
}


function getNextImageTwo() {
    if (random_display) {
        imageNumTwo = randNum(0, totalImagesTwo-1);
    }
    else {
        imageNumTwo = (imageNumTwo+1) % totalImagesTwo;
    }   

    var new_image = get_ImageItemLocation(imageArrayTwo[imageNumTwo]);
return(new_image);
}


function getNextImageThree() {
    if (random_display) {
        imageNumThree = randNum(0, totalImagesTwo-1);
    }
    else {
        imageNumThree = (imageNumThree+1) % totalImagesThree;
    }   

    var new_image = get_ImageItemLocation(imageArrayThree[imageNumThree]);
return(new_image);
}


function prevImage(place) {
    var new_image = getPrevImage();
    document[place].src = new_image;
}

function switchImage(place) {
    var new_image = getNextImage();
    document[place].src = new_image;
    var recur_call = "switchImage('"+place+"')";
    timerID = setTimeout(recur_call, interval);
    }

function switchImageTwo(place) {
    var new_image = getNextImageTwo();
    document[place].src = new_image;
    var recur_call = "switchImageTwo('"+place+"')";
    timerID = setTimeout(recur_call, interval);
    }


function switchImageThree(place) {
    var new_image = getNextImageThree();
    document[place].src = new_image;
    var recur_call = "switchImageThree('"+place+"')";
    timerID = setTimeout(recur_call, interval);
    }


</script>




  </head>

<body>


<div id="content">
<div id="title"><a href="mailto:hello@a.com"><center><img src="imgs/adil.gif" alt="A D I L" width="293" height="115" border="0"></center></a></div>
<div id="books">
  <p><a href="#" onclick="switchImage('slideImg'); clearTimeout(timerID)"><img src="imgs/1.jpg" name="slideImg" width="300" height="427" border="0"></a>
  <img src="imgs/px.gif" width="40">
  <a href="#" onclick="switchImageTwo('slideImg2'); clearTimeout(timerID)"><img src="imgs/t1.jpg" name="slideImg2" width="300" height="423" border="0"></a>
  <img src="imgs/px.gif" width="40">
  <a href="#" onclick="switchImageThree('slideImg3'); clearTimeout(timerID)"><img src="imgs/th1.jpg" name="slideImg3" width="300" height="427" border="0"></a>
  <img src="imgs/px.gif" width="993" height="40"></p>

1 个答案:

答案 0 :(得分:0)

我认为您只需要从Click处理程序调用的任何函数返回false。