如何在不创建新部分的情况下只保留覆盖页面的雪效果?
How do I only keep the snow effect to cover my page without creating a new section?
我新的html和我使用这个在我的js文件,我不确定如何编辑这样的代码,我只能得到的效果只有不创建画布…,只是为了覆盖我当前的内容。
function vp(woh)
{
var viewportwidth;
var viewportheight;
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth,
viewportheight = window.innerHeight
}
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
viewportwidth = document.documentElement.clientWidth,
viewportheight = document.documentElement.clientHeight
}
else
{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
viewportheight = document.getElementsByTagName('body')[0].clientHeight
}
if (woh == 'w')
{
return viewportwidth;
}
else if (woh == 'h')
{
return viewportheight;
}
else
{
return false;
}
}
function snowflake()
{
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.radius = Math.random()*2;
this.color = "white";
var tobefallingSpeed = Math.random() * 100;
this.fallingSpeed = tobefallingSpeed + 30;
}
function render()
{
ctx.clearRect(0,0,canvas.width, canvas.height);
for (b=0;b<snowflakes;b++)
{
sf[b].y+=0.4;
if(sf[b].y> canvas.height){
sf[b].y = 0;
}
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(sf[b].x,sf[b].y,sf[b].radius,sf[b].radius);
}
}
function main()
{
now = Date.now();
delta = now - then;
render();
then = now;
}
then = Date.now();
var int = self.setInterval(main,1);
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
canvas.width = vp('w');
canvas.height = vp('h');
document.body.appendChild(canvas);
var numberofSnowflakes = 100;
var sf = [];
for (i=0;i<numberofSnowflakes;i++)
{
sf[i] = new snowflake();
snowflakes = i;
}
我不记得这段代码是从哪里来的(或者我自己写了多少),所以如果有人想要功劳,我很乐意给他,但如果你只是想要一些没有画布的雪花,你可以试试这个:
<div id="snowflakes"> [your current content goes here] </div>
//set background to transparent
var snowCount = 0;
function snowFlakes(){
var randomTime = Math.floor(Math.random() * (500) * 2);
setTimeout(function(){
snowCount = snowCount +2;
jquerysnow();
snowFlakes();
},randomTime);
}
function jquerysnow() {
var fs = Math.floor(Math.random() * (30 - 20) + 20),
snow = $('<div class="snow" style="font-size:'+fs+'px;"></div>'),
dH = $(document).height() + 'px',
sf = $('#snowflakes');
sf.prepend(snow);
snowX = Math.floor(Math.random() * sf.width());
snowSpd = Math.floor(Math.random() * (500) * 20);
snow.css({'left':snowX+'px'});
snow.html('❄');
snow.animate({top : dH, opacity : '1',}, 9000, function(){
$(this).remove();
});
}
snowFlakes();
</script>
相关文章:
- 分派点击事件并保留击键修饰符
- 在循环中分配json值时,值被覆盖
- 谷歌地图固定位置覆盖
- 只覆盖箭头键滚动事件
- 如何使用jquery强制一个单词更改大小写等以保留品牌
- 提交后保留下拉选择的值
- JQuery覆盖不更改单选选项
- 如何覆盖锚点元素's href目标,并在我点击转到目标javascript时删除其他错误
- 刷新后保留对网页的更改
- 在mvc应用程序中,在回发时保留最初隐藏的文本框的隐藏或可见状态
- Flash Uploadify在调用我的MVC控制器时没有保留会话/授权
- 当设置addFromAutocompleteOnly时,剩余文本将保留在输入字段中
- 将添加一个相同类型的事件附加或覆盖以前添加的具有相同名称的事件
- 如何在 JavaScript 中覆盖全局函数,但保留对原始函数的引用
- JQuery变量在循环中覆盖,希望保留不同的值
- 覆盖保留字(设置 false = true)
- 如何在不创建新部分的情况下只保留覆盖页面的雪效果?
- 词法作用域变量在getJSON调用中被覆盖后仍保留其值
- Javascript-覆盖console.log并保留旧函数
- 关闭后仍保留灯箱覆盖,页面高度为100%