javascript 用图像替换按钮文本...用外部事件触发按钮

发布于 2024-12-01 12:51:12 字数 2165 浏览 1 评论 0原文

我找到了这个秒表 javascript,带有“开始”、“停止”和“重置”按钮。这个脚本的功能很棒,但我想对其进行一些修饰。我已经设法使用 CSS border-image 设置按钮背景的样式,但我想使用 javascript 用图标替换“开始”、“停止”和“重置”文本。

我还想让计时器在用户单击另一个区域时启动。例如,我可以用它来跟踪用户解决谜题需要多长时间……因此,如果可以在用户记录在谜题上的第一个事件时激活“开始”按钮,那就太好了。我想为任何点击创建一个事件侦听器,但恐怕这可能会导致按钮在每个事件上切换“开始/停止”。

最后,如果“停止”按钮触发一个隐藏的 div,并在游戏暂停时在拼图顶部显示一个消息屏幕,那就太好了。下面是我的代码片段

JAVASCRIPT

var sec = 0;
var min = 0;
var hour = 0;
function stopwatch(text) {
sec++;
if (sec == 60) {
sec = 0;
 = min + 1; }
else {
min = min; }
if (min == 60) {
min = 0; 
hour += 1; }

if (sec<=9) { sec = "0" + sec; }
document.clock.stwa.value = ((hour<=9) ? "0"+hour : hour) + " : " + ((min<=9) ? "0" +        min : min) + " : " + sec;

if (text == "Start") { document.clock.theButton.value = "Stop";}
if (text == "Stop") { document.clock.theButton.value = "Start"; }

if (document.clock.theButton.value == "Start") {
window.clearTimeout(SD);
alert("Timer is Paused...Resume?");
return true; }
SD=window.setTimeout("stopwatch();", 1000);
}

function resetIt() {
sec = -1;
min = 0;
hour = 0;
if (document.clock.theButton.value == "Stop") {
document.clock.theButton.value = "Start"; }
window.clearTimeout(SD);
}

CSS

.button {
border-width:0 5px; -webkit-border-image:url(../images/toolButton-791569.png) 0 5 0 5;   padding:7px 0; font: bold 12px Arial,sans-serif; color:#fff; width:auto; margin:5px 10px 0 0; width:auto; height: 25px; text-align:center; cursor:pointer;
}

HTML

   <div id="timer">
   <form name="clock">
   <input type="text" size="14" name="stwa" value="00 : 00 : 00" style="text-align:center; position: relative; top: 0px; background: #ccc; font-weight: bold; font-size: 14px;" />
   <br/><input type="button" class="button" name="theButton" onClick="stopwatch(this.value);" value="Start" />
   <input type="button" class="button" value="Reset" onClick="resetIt();reset();" />
   </form></div>

我确信这个社区中的优秀程序员可能会发现这个问题很微不足道,但是请原谅我。我是一名新手程序员,这个挑战让我度过了许多个不眠之夜。请帮忙...

谢谢, 卡洛斯

I found this stopwatch javascript, complete with "Start", "Stop" and "Reset" buttons. The functionality of this script is great, but I would like to spruce it up a bit. I've managed to style the button background with CSS border-image, but I want to use javascript to replace the "Start", "Stop" and "Reset" text with icons.

I would also like to make the timer start when the user clicks on another area. For example, I may use this to track how long a user takes to solve a puzzle...so it would be great if the "Start" button could be activated upon the user's FIRST event recorded on the puzzle. I though about making an event listener for any click, but I'm afraid that may cause the button to toggle Start/Stop on each event.

Lastly, it would be nice if the "Stop" button triggered a hidden div with a message screen that shows up on top of the puzzle while the game is paused. Below is a snippet of my code

JAVASCRIPT

var sec = 0;
var min = 0;
var hour = 0;
function stopwatch(text) {
sec++;
if (sec == 60) {
sec = 0;
 = min + 1; }
else {
min = min; }
if (min == 60) {
min = 0; 
hour += 1; }

if (sec<=9) { sec = "0" + sec; }
document.clock.stwa.value = ((hour<=9) ? "0"+hour : hour) + " : " + ((min<=9) ? "0" +        min : min) + " : " + sec;

if (text == "Start") { document.clock.theButton.value = "Stop";}
if (text == "Stop") { document.clock.theButton.value = "Start"; }

if (document.clock.theButton.value == "Start") {
window.clearTimeout(SD);
alert("Timer is Paused...Resume?");
return true; }
SD=window.setTimeout("stopwatch();", 1000);
}

function resetIt() {
sec = -1;
min = 0;
hour = 0;
if (document.clock.theButton.value == "Stop") {
document.clock.theButton.value = "Start"; }
window.clearTimeout(SD);
}

CSS

.button {
border-width:0 5px; -webkit-border-image:url(../images/toolButton-791569.png) 0 5 0 5;   padding:7px 0; font: bold 12px Arial,sans-serif; color:#fff; width:auto; margin:5px 10px 0 0; width:auto; height: 25px; text-align:center; cursor:pointer;
}

HTML

   <div id="timer">
   <form name="clock">
   <input type="text" size="14" name="stwa" value="00 : 00 : 00" style="text-align:center; position: relative; top: 0px; background: #ccc; font-weight: bold; font-size: 14px;" />
   <br/><input type="button" class="button" name="theButton" onClick="stopwatch(this.value);" value="Start" />
   <input type="button" class="button" value="Reset" onClick="resetIt();reset();" />
   </form></div>

I'm sure the brilliant programmers in this community might find this question to be trivial, but please forgive me. I am a novice programmer and this challenge is causing me many sleepless nights. Please help...

Thanks,
Carlos

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2024-12-08 12:51:12

给你 http://jsfiddle.net/mplungjan/C4wjV/

我没有按暂停按钮

<style type="text/css">
img { height:50px }
#stwa {text-align:center; position: relative; top: 0px; background: #ccc; font-weight: bold; font-size: 14px;}
</style>

<script type="text/javascript">
var buttons = {
    Start:"http://morethanvoice.net/m1/img/play.gif",
    Stop:"http://morethanvoice.net/m1/img/pause.gif"
}
var sec = 0,min = 0,hour = 0,SD;

function stopwatch(text) {
  sec++;
  if (sec === 60) {
    sec = 0;
    min += 1;
  }
  if (min === 60) {
    min = 0;
    hour += 1;
  }

  document.clock.stwa.value = ((hour<10) ? "0"+hour : hour) + " : " +
        ((min<10) ? "0" + min : min) + " : " +
        ((sec<10)? "0" + sec:sec);

  if (text) { // i.e. we are clicked
    if (text==="Stop") window.clearTimeout(SD);
    text = (text==="Start")?"Stop":"Start";
    document.getElementById('stopstart').title=text;
    document.getElementById('stopstarticon').src=buttons[text];  
  }
    if (!text || text === "Stop") SD=window.setTimeout(function() {stopwatch()}, 1000);
  return false;
}

function resetIt() {
  sec = -1;
  min = 0;
  hour = 0;
  document.getElementById('stopstart').title = "Start"
  document.getElementById('stopstarticon').src=buttons["Start"];
  window.clearTimeout(SD);
  document.clock.stwa.value=document.clock.stwa.defaultValue;
  return false;
}
</script>

    <form name="clock"><input type="text" size="14" id="stwa" name="stwa" value="00 : 00 : 00"  />
</form><br/>

<a href="#" id="stopstart" title="Start"
onClick="return stopwatch(this.title)"><img id="stopstarticon" src="http://morethanvoice.net/m1/img/play.gif" border="0" /></a>
<a href="#" onClick="return resetIt()"><img src="http://morethanvoice.net/m1/img/rewind.gif" border="0" /></a>

Here you go http://jsfiddle.net/mplungjan/C4wjV/

I did not do the pause button

<style type="text/css">
img { height:50px }
#stwa {text-align:center; position: relative; top: 0px; background: #ccc; font-weight: bold; font-size: 14px;}
</style>

<script type="text/javascript">
var buttons = {
    Start:"http://morethanvoice.net/m1/img/play.gif",
    Stop:"http://morethanvoice.net/m1/img/pause.gif"
}
var sec = 0,min = 0,hour = 0,SD;

function stopwatch(text) {
  sec++;
  if (sec === 60) {
    sec = 0;
    min += 1;
  }
  if (min === 60) {
    min = 0;
    hour += 1;
  }

  document.clock.stwa.value = ((hour<10) ? "0"+hour : hour) + " : " +
        ((min<10) ? "0" + min : min) + " : " +
        ((sec<10)? "0" + sec:sec);

  if (text) { // i.e. we are clicked
    if (text==="Stop") window.clearTimeout(SD);
    text = (text==="Start")?"Stop":"Start";
    document.getElementById('stopstart').title=text;
    document.getElementById('stopstarticon').src=buttons[text];  
  }
    if (!text || text === "Stop") SD=window.setTimeout(function() {stopwatch()}, 1000);
  return false;
}

function resetIt() {
  sec = -1;
  min = 0;
  hour = 0;
  document.getElementById('stopstart').title = "Start"
  document.getElementById('stopstarticon').src=buttons["Start"];
  window.clearTimeout(SD);
  document.clock.stwa.value=document.clock.stwa.defaultValue;
  return false;
}
</script>

    <form name="clock"><input type="text" size="14" id="stwa" name="stwa" value="00 : 00 : 00"  />
</form><br/>

<a href="#" id="stopstart" title="Start"
onClick="return stopwatch(this.title)"><img id="stopstarticon" src="http://morethanvoice.net/m1/img/play.gif" border="0" /></a>
<a href="#" onClick="return resetIt()"><img src="http://morethanvoice.net/m1/img/rewind.gif" border="0" /></a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文