使用键盘和鼠标更改字体大小
Changing font size using keyboard and mouse
我正试图通过按下某些按钮来更改字体大小。例如:
- "Alt"answers"+"
- Esc
当涉及到Esc按钮时,我遇到了问题。我知道Esc键Code是27,但它似乎不起作用。我甚至试着使用onkeyup,但也无济于事。基本上,当我按下Esc按钮时,它应该将文本返回到30pt的起始位置。如果我能回答这个问题,我将不胜感激。
<html>
<head>
<style>p{font-size: 30pt;}</style>
</head>
<body onmousedown="changeSize(event)" onkeyup="changeSize(event)">
<p>Text1</p>
<p>Text2</p>
<p>Text3</p>
</body>
<script>
var start = 30;
var min = 10;
var max = 50;
function changeSize(e) {
var b = e.keyCode;
var c = e.button;
var fontSized = document.getElementsByTagName("p");
if (e.altKey){
if (b == 107){
if (start <=max && start >=min){
start += 5;
for (i=0; i<=fontSized.length; i++){
fontSized[i].style.fontSize = start + "pt";
};
}
}
}
if (b == 27){
start = 30;
for (i=0; i<=fontSized.length; i++){
fontSized[i].style.fontSize = start + "pt";
};
}
}
</script>
</html>
您的if (b == 27) {
位于if (e.altKey) {
块内,因此只有同时按下alt时它才会工作。你的代码应该是这样的:
if (e.altKey){
if (b == 107){
if (start <=max && start >=min){
start += 5;
for (i=0; i<=fontSized.length; i++){
fontSized[i].style.fontSize = start + "pt";
};
}
}
}
if (b == 27){
start = 30; // start == 30 is a comparator, and does not set start to 30
for (i=0; i<=fontSized.length; i++){
fontSized[i].style.fontSize = start + "pt";
};
}
注意,if (b == 27)
之后的start == 30
已更改为start = 30
编辑:尝试代码后,一切似乎都正常工作,但要增加字体大小,必须执行Alt+'+'(在数字键盘上!)。您可能需要考虑使用Alt+Shift+'='或只使用Alt+'='
作为参考,这里是我的代码:
<html>
<head>
<style>p{font-size: 30pt;}</style>
</head>
<body onmousedown="changeSize(event)" onkeyup="changeSize(event)">
<p>Text1</p>
<p>Text2</p>
<p>Text3</p>
</body>
<script>
var start = 30;
var min = 10;
var max = 50;
function changeSize(e) {
var b = e.keyCode;
var c = e.button;
var fontSized = document.getElementsByTagName("p");
if (e.altKey) {
if (b == 187){ // Now the combination is Alt + '='
if (start <=max && start >=min){
start += 5;
for (i=0; i<=fontSized.length; i++){
fontSized[i].style.fontSize = start + "pt";
};
}
}
}
if (b == 27){
start = 30;
for (i=0; i<=fontSized.length; i++){
fontSized[i].style.fontSize = start + "pt";
};
}
}
</script>
</html>
在这种情况下,通常需要阻止事件传播,因为它将遍历所有父级,直到到达浏览器。
相关文章:
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- EaseJS拖放;放下(动画CC)电影剪辑的鼠标坐标
- 当鼠标悬停在文本中的单词上时显示警报
- JsFiddle上的鼠标事件不起作用
- 用Javascript更改我网站上的字体大小
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 鼠标悬停事件影响列表中所有行中的按钮,而不仅仅是特定按钮
- 不同字体的字母间距不同
- 如何在谷歌字体加载时显示加载图像
- 将鼠标旋转限制为特定的度数
- 跟踪jqplot垂直折线图的鼠标位置
- PhoneGap选项卡栏自定义字体,背景图案
- 如何在鼠标输入时对字体大小进行动画处理
- 使用键盘和鼠标更改字体大小
- 更改鼠标悬停时的字体颜色,然后单击javascript或html
- 鼠标悬停时更改字体大小,鼠标离开时恢复为原始字体
- 鼠标移到上面时更改字体,第二次移到上面时恢复字体
- HighCharts在鼠标悬停时改变dataLabel's字体大小
- 鼠标悬停时在图像中心显示图标字体