在表单body的初始化事件中添加下面的代码,可以实现动态切换背景图,不过有点小问题,第一次切换时背景变空白了,后面就正常了,我还没找到原因。把背景图片放到项目下,表单的主体背景是将图片转化为base64格式存储到文件中的,所以需要做一下转换。- var img1 = "http://localhost:8075/WebReport/back1.png";//imgurl 就是你的图片路径
- var img2 = "http://localhost:8075/WebReport/back2.png";
- var image = new Image();
- var base64 = '';
- var flag = 1;
- setInterval(function()
- {
- if(flag == 1)
- {
- image.src = img1;
- image.onload = function(){
- base64 = getBase64Image(image);
- }
- $('.widgetBackground').css('background','url("'+base64+'")');
- flag = 2;
- }
- else
- {
- image.src = img2;
- image.onload = function(){
- base64 = getBase64Image(image);
- }
- $('.widgetBackground').css('background','url("'+base64+'")');
- flag = 1;
- }
- },10000);
- function getBase64Image(img) {
- var canvas = document.createElement("canvas");
- canvas.width = img.width;
- canvas.height = img.height;
- var ctx = canvas.getContext("2d");
- ctx.drawImage(img, 0, 0, img.width, img.height);
- var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
- var dataURL = canvas.toDataURL("image/"+ext);
- return dataURL;
- }
复制代码
编辑于 2017-8-10 17:30
|