gpt4 book ai didi

javascript - 创建一个计算浏览器视口(viewport)尺寸的动画

转载 作者:太空宇宙 更新时间:2023-11-03 18:15:34 25 4
gpt4 key购买 nike

我目前有一个返回浏览器窗口高度和宽度的小脚本。我遇到的麻烦是实现一种高度和宽度从零到当前负载尺寸的计数方法。它的功能类似于: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);
}

最佳答案

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;
}
}

});

关于javascript - 创建一个计算浏览器视口(viewport)尺寸的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22904205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com