简体   繁体   English

使用javascript作为背景图片

[英]Use javascript for a background image

I have looking other post for this problem (because there are, of course). 我正在寻找其他帖子来解决这个问题(因为那里当然有)。 But I even didn't succeed with the answer of these other post. 但是我什至没有成功回答其他帖子。

Then, my issue : 然后,我的问题:

I have the following script to open an image randomly 我有以下脚本来随机打开图像

<script> function banner()
{var img = new Array();
img[0]='images/hochbau1.jpg';
img[1]='images/hochbau2.jpg';
img[2]='images/hochbau3.jpg';
img[3]='images/hochbau4.jpg';
img[4]='images/hochbau5.jpg';
img[5]='images/hochbau6.jpg';
img[6]='images/hochbau7.jpg';
img[7]='images/hochbau8.jpg';
img[8]='images/hochbau9.jpg';
img[9]='images/hochbau10.jpg';
var n=Math.round((Math.random()*9));
document.write(img[n]);
} </script> 

Then, I would like to have this images in this (instead of images/hochbau.jpg) : 然后,我想在其中使用这些图像(而不是images / hochbau.jpg):

 <div class="section22" style="background-image:url(images/hochbau.jpg); "> </div>

Then, I tried this: 然后,我尝试了这个:

 <div class="section22" style="background-image:url(<script> banner(); </script>);"> </div> 

or this 或这个

<div class="section22" style="background-image:url(
                   <script type="text/javascript" language="javascript">
                       document.write('images/hochbau'+ Math.round((Math.random()*9)+1)+ '.jpg');
                  </script>
                  ); "> </div>

Then, I changed the script as : 然后,我将脚本更改为:

<script> function banner()
{var img = new Array();
img[0]='images/hochbau1.jpg';
img[1]='images/hochbau2.jpg';
img[2]='images/hochbau3.jpg';
img[3]='images/hochbau4.jpg';
img[4]='images/hochbau5.jpg';
img[5]='images/hochbau6.jpg';
img[6]='images/hochbau7.jpg';
img[7]='images/hochbau8.jpg';
img[8]='images/hochbau9.jpg';
img[9]='images/hochbau10.jpg';
var n=Math.round((Math.random()*9));
document.getElementById('myPElement').style.backgroundImage = 'url(img[n])';

} </script> 

with the following 与以下

<div class="section22" id="myPElement">

But nothing is working..... maybe I am closed to the solution but I am stuck... 但是什么都没有起作用.....也许我不了解该解决方案,但是我被困住了...

if you have some observation, please tell me ! 如果您有任何观察,请告诉我! many thanks for your help 非常感谢您的帮助

You are just not referencing the image correctly in your script 您只是在脚本中没有正确引用图像

<script> function banner()
{var img = new Array();
img[0]='images/hochbau1.jpg';
img[1]='images/hochbau2.jpg';
img[2]='images/hochbau3.jpg';
img[3]='images/hochbau4.jpg';
img[4]='images/hochbau5.jpg';
img[5]='images/hochbau6.jpg';
img[6]='images/hochbau7.jpg';
img[7]='images/hochbau8.jpg';
img[8]='images/hochbau9.jpg';
img[9]='images/hochbau10.jpg';
var n=Math.round((Math.random()*9));
document.getElementById('myPElement').style.backgroundImage = 'url(' + img[n] +')';  //you are passing img[n] as string, just change this

} </script> 

EDIT: I see your page: http://www.mytmedia.craym.eu/HRPwebsite/index.php 编辑:我看到您的页面: http : //www.mytmedia.craym.eu/HRPwebsite/index.php

from the code inspect I see banner() function is never executed, and also if I try to execute it in the console the backround image is set correcty but your iamges are not there: 从代码检查中,我看到banner()函数从未执行过,而且如果我尝试在控制台中执行它,则背景图像设置正确,但是您的图像不存在: 在此处输入图片说明

这应该工作

document.getElementById("myPElement").style.backgroundImage = "url("+img[n]+")";

hi and many thanks for your help.... 嗨,非常感谢您的帮助。

The things which was wrong in my code was : 1- using a function in script. 我的代码中出错的地方是:1-在脚本中使用了函数。 I had to just using script but without "function" 2- and of course the form 'url(img[n])' wich was wrong.. 我只需要使用脚本但不使用“功能” 2,当然“ url(img [n])”的形式是错误的。

The code wich work for me (i have tested on local, i had to test it online) 最适合我的代码(我已经在本地测试过,我不得不在线测试过)

                <div id="myPElement" class="section22" ></div>
            <script> 
                var img = new Array();
                img[0]='http://localhost:8888/images/hochbau1.jpg';
                img[1]='http://localhost:8888/images/hochbau2.jpg';
                img[2]='http://localhost:8888/images/hochbau3.jpg';
                img[3]='http://localhost:8888/images/hochbau4.jpg';
                img[4]='http://localhost:8888/images/hochbau5.jpg';
                img[5]='http://localhost:8888/images/hochbau6.jpg';
                img[6]='http://localhost:8888/images/hochbau7.jpg';
                img[7]='http://localhost:8888/images/hochbau8.jpg';
                img[8]='http://localhost:8888/images/hochbau9.jpg';
                img[9]='http://localhost:8888/images/hochbau10.jpg';
                var n=Math.round((Math.random()*9));
                document.getElementById("myPElement").style.backgroundImage = "url(" + img[n] +")";
            </script> 

Evrything is working now.... the photos appear randomly.... Evrything正在运行...。照片随机出现。

Many thank again for your help ! 非常感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM