如何在javascript/html中调整图像大小 平行轧制 *{ 边际:0px; 填充:0px; } #形象{ 位置:相对位置; z指数:-1 } #内容{ 高度:750px; 宽度:100%; 利润上限:-10px; 背景色:#0d0d; 位置:相对位置; z指数:1; } var-ypos,图像; 函数parallex(){ image=document.getElementById('image'); ypos=window.pageYOffset; image.style.top=ypos*.7+'px'; } window.addEventListener('scroll',parallex),false; var imlocation=“img/”; var currentdate=0; var图像_编号=0; 函数ImageArray(n){ 这个长度=n; 对于(var i=1;i

如何在javascript/html中调整图像大小 平行轧制 *{ 边际:0px; 填充:0px; } #形象{ 位置:相对位置; z指数:-1 } #内容{ 高度:750px; 宽度:100%; 利润上限:-10px; 背景色:#0d0d; 位置:相对位置; z指数:1; } var-ypos,图像; 函数parallex(){ image=document.getElementById('image'); ypos=window.pageYOffset; image.style.top=ypos*.7+'px'; } window.addEventListener('scroll',parallex),false; var imlocation=“img/”; var currentdate=0; var图像_编号=0; 函数ImageArray(n){ 这个长度=n; 对于(var i=1;i,javascript,html,Javascript,Html,首先,您的代码运行不正确,原因如下: <!doctype html> <html> <head> <title>ParallecScrolling</title> <style type="text/css"> *{ margin: 0px; padding: 0px; } #image { position: relative; z-index: -1 } #content { height: 75

首先,您的代码运行不正确,原因如下:

<!doctype html>
<html>
<head>
<title>ParallecScrolling</title>
<style type="text/css">
*{
margin: 0px;
padding: 0px;
}
#image {
    position: relative;
    z-index: -1
}
#content {
    height: 750px;
    width: 100%;
    margin-top:-10px;
    background-color:#0d0d0d;
    position: relative;
    z-index: 1;
}
</style>


<body>
<!-- <img id="image" src="img/bg.jpg" height="710px" width="100%" /> -->
<script type="text/javascript">
    var ypos,image;
    function parallex() {
        image = document.getElementById('image');
        ypos = window.pageYOffset;
        image.style.top = ypos * .7+ 'px';
    }
    window.addEventListener('scroll', parallex),false;

</script>
</head>

<script type="text/javascript">
var imlocation = "img/";
 var currentdate = 0;
 var image_number = 0;
 function ImageArray (n) {
   this.length = n;
   for (var i =1; i <= n; i++) {
     this[i] = ' '
   }
 }
 image = new ImageArray(6)
 image[0] = 'bg.jpg'
 image[1] = 'bg2.jpg'
 image[2] = 'bg3.jpg'
 image[3] = 'bg4.jpg'
 image[4] = 'bg5.jpg'
 image[5] = 'bg6.jpg'

 var rand = 60/image.length
 function randomimage() {
 currentdate = new Date()
  image_number = currentdate.getSeconds()
  image_number = Math.floor(image_number/rand)
  return(image[image_number])
   }
      var insert = imlocation + randomimage();
   document.write("<img src='" + insert+ "'>");
</script>
<div id="content"></div>

</body>
可能会注意到,缺少分号意味着上面的每个语句都没有结束。上面的语句应该是:

image = new ImageArray(6)
image[0] = 'bg.jpg'
image[1] = 'bg2.jpg'
image[2] = 'bg3.jpg'
image[3] = 'bg4.jpg'
image[4] = 'bg5.jpg'
image[5] = 'bg6.jpg'

var rand = 60/image.length
function randomimage() {
currentdate = new Date()
image_number = currentdate.getSeconds()
image_number = Math.floor(image_number/rand)
return(image[image_number])
如果您在整理完这些后有任何错误,请告诉我,然后我将相应地改进我的答案!:)

第2部分:

与此相反:

image = new ImageArray(6);
image[0] = 'bg.jpg';
image[1] = 'bg2.jpg';
image[2] = 'bg3.jpg';
image[3] = 'bg4.jpg';
image[4] = 'bg5.jpg';
image[5] = 'bg6.jpg';

var rand = 60/image.length;
function randomimage() {
currentdate = new Date();
image_number = currentdate.getSeconds();
image_number = Math.floor(image_number/rand);
return(image[image_number]);

把它放在内联,它就会工作。即使我使用CSS设置自定义宽度失败。除非有人有一个好的解决方案。!!!CSS解决方案必须工作。a.你的html很凌乱,你的
正文中有
,你没有关闭
html
标签。B.到底是什么问题?你能看到图像吗没有正确的宽度和高度?或者你根本看不到它们?你能用它发布一个小提琴(不仅仅是代码)吗?问题是当我尝试设置插入图像大小时,没有图像显示。好的,我按照你告诉我的做了。我没有遇到错误,但我的图像仍然没有重新调整大小。
var insert = imlocation + randomimage();
insert.height = "710px";
insert.width = "100%";
document.write("<img src='" + insert+ "'/>");
var insert = imlocation + randomimage();
document.write("<img style='height:710px;width:100%' src='" + insert + "'/>");
var insert = imlocation + randomimage();
document.write("<img class='ourclass' src='" + insert + "'/>");
.ourclass {
     height:710px;
     width:100%
}