You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.9 KiB

3 years ago
  1. function win(attr) { //获取可视区尺寸,参数为height|width
  2. // 横屏
  3. var detectOrient = function() {
  4. var width = document.documentElement.clientWidth,
  5. height = document.documentElement.clientHeight,
  6. wrapper = document.getElementById("wrap"),
  7. style = "";
  8. if(width >= height) { // 竖屏
  9. style += "width:100%";
  10. style += "height:100%;";
  11. style += "-webkit-transform: rotate(0); transform: rotate(0);";
  12. style += "-webkit-transform-origin: 0 0;";
  13. style += "transform-origin: 0 0;";
  14. } else { // 横屏
  15. style += "width:" + height + "px;"; // 注意旋转后的宽高切换
  16. style += "height:" + width + "px;";
  17. style += "-webkit-transform: rotate(90deg); transform: rotate(90deg);";
  18. // 注意旋转中点的处理
  19. style += "-webkit-transform-origin: " + width / 2 + "px " + width / 2 + "px;";
  20. style += "transform-origin: " + width / 2 + "px " + width / 2 + "px;";
  21. }
  22. wrapper.style.cssText = style;
  23. }
  24. window.onresize = detectOrient;
  25. detectOrient();
  26. }
  27. window.onload = function() {
  28. var bd = document.getElementsByClassName('snowList'); //获取body
  29. function snow() { //雪花函数
  30. var win_height = '100%'; //获取窗口高度
  31. var win_width = document.documentElement.clientHeight; //获取窗口宽度
  32. function createsnow() { //创建雪花函数
  33. var snowdiv = document.createElement('div'); //新建一个div
  34. snowdiv.className = 'snow'
  35. snowdiv.innerHTML = "j"; //写入雪花(字体的代码为j)
  36. var size = 20 + parseInt(Math.random() * 30); //随机生成雪花的大小
  37. var left = parseInt(win_width * (Math.random()) * 0.98); //随机生成left值
  38. snowdiv.style.fontSize = size + 'px'; //应用字体大小
  39. snowdiv.style.width = size + 'px'; //div应用宽度
  40. snowdiv.style.height = size + 'px'; //div应用高度
  41. snowdiv.style.left = left + 'px'; //雪花的left值
  42. $('.snowList').append(snowdiv)
  43. // bd.append(snowdiv); //将此雪花放入文档中
  44. return snowdiv; //返回这个雪花对象
  45. }
  46. setInterval(function() {
  47. var snow = createsnow(); //创建雪花并得到这一对象
  48. var sbd = Math.ceil(Math.random() * 10); //随机生成下落速度
  49. move(snow, sbd); //移动雪花
  50. }, 100);
  51. function move(obj, speed) { //移动函数
  52. var top = 0;
  53. var timer = setInterval(
  54. function() {
  55. top = top + speed; //改变top值
  56. obj.style.top = top + 'px';
  57. if(top > win_height - 20) {
  58. clearInterval(timer); //下落停止
  59. }
  60. }, 30);
  61. }
  62. setInterval(function() { //一段时间之后清理一次雪花,防止文档过大卡住
  63. var snows = document.getElementsByClassName('snow');
  64. for(var i = 0; i < snows.length / 3; i++) {
  65. snows[i].parentNode.removeChild(snows[i]);
  66. }
  67. }, 5000);
  68. }
  69. snow(); //执行snow函数
  70. window.onresize = function() { //窗口改变
  71. var snows = document.getElementsByTagName('div');
  72. snows.className="snow"
  73. for(var i = 0; i < snows.length; i++) {
  74. snows[i].parentNode.removeChild(snows[i]);
  75. }
  76. }
  77. }