创建一个可以计算浏览器视口尺寸的动画

时间:2014-04-07 05:24:59

标签: javascript jquery html css

我目前有一个小的脚本,它返回浏览器窗口的高度和宽度。我遇到的问题是实现一种高度和宽度从零开始计数到加载时当前尺寸的方法。它的功能类似于:http://jsfiddle.net/YWn9t/,但目标数量将是上面提到的尺寸。使用甚至比jsfiddle更少的代码可以完成吗?

HTML:

<div>
    <span></span>
    <span></span>
</div>

CSS:

body {text-align: center;}

div {border: 2px solid #000; display: inline-block; padding: 5px 20px; vertical-align: middle;}

span:first-of-type:after {content: "|"; margin: 0 10px;}

jQuery的:

$(document).ready(function (e) {
    showViewportSize();
});

$(window).resize(function (e) {
    showViewportSize();
});

function showViewportSize() {
    var the_width = $(window).width();
    var the_height = $(window).height();
    $('span:first-of-type').text(the_width);
    $('span:last-of-type').text(the_height);
}

1 个答案:

答案 0 :(得分:0)

FIDDLE

JS

$('#start').on('click', function(){

  var windowheight = $(window).height();
  var windowwidth  = $(window).width();
  $('.putmehere1').html('height: ' + 
                         windowheight + 
                         'width: ' + 
                        windowwidth);

  var startwidth = 0;
  var endwidth = windowwidth;
  var startheight = 0;
  var endheight = windowheight;
  var timer;
  var timer2;

timedCount();
timedCount2();


function timedCount()
{
   startwidth = startwidth + 1;
   timer = setTimeout(function(){timedCount()}, 20);
   $('.putwidth').html(startwidth);
   if (startwidth == endwidth)
      {
       clearTimeout(timer);
       startwidth = endwidth;
       }
}

function timedCount2()
{
   startheight = startheight + 1;
   timer2 = setTimeout(function(){timedCount2()}, 20);
   $('.putheight').html(startheight);
   if (startheight == endheight)
   {
       clearTimeout(timer2);
       startheight = endheight;
    }
}

 });