按下向上或向下按钮时,我正试图加快默认的向下/向上行为。整个页面应该向上或向下动画。我怎么能这样做?
我的代码:
$(document).keydown(function(e) {
switch(e.which) {
case 38: // up
$('html').animate({ scrollTop: $('html').scrollTop()-500 }, 250);
break;
case 40: // down
$('html').animate({ scrollTop: $('html').scrollTop()+500 }, 250);
break;
}
e.preventDefault();
});
答案 0 :(得分:1)
如果要滚动视口高度而不是整页,请使用<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>\n";
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " .
socket_strerror(socket_last_error()) . "\n";
} else {
echo "OK.\n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false) {
echo "socket_connect() failed.\nReason: ($result) " .
socket_strerror(socket_last_error($socket)) . "\n";
} else {
echo "OK.\n";
}
$in = "HEAD / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";
echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
echo "Closing socket...";
socket_close($socket);
echo "OK.\n\n";
?>
$(window).height()
答案 1 :(得分:0)
没关系。得到它了。几乎。
现在的问题是,preventdefault应该只适用于向上/向下键......
$(document).keydown(function(e) {
var $body = $('body');
switch(e.which) {
case 38: // up
$body.animate({ scrollTop: $body.scrollTop() - $body.height() }, 250);
console.log()
break;
case 40: // down
$body.animate({ scrollTop: $body.scrollTop() + $body.height() }, 250);
break;
}
e.preventDefault();
});