简体   繁体   English

带有上一个,暂停和下一个按钮的JS文本循环

[英]JS Text Loop with Previous, Pause & Next Button

I have the below code with me for Text with Next & Previous Button. 我下面的代码带有下一个和上一个按钮的文本。 I want this to loop repeatedly on page load and one button for pause in between the both. 我希望它在页面加载和一个按钮在两者之间暂停时反复循环。 All three buttons to be totally on the right side on the same line of displaying the text. 这三个按钮全部位于显示文本的同一行的右侧。 Please support me.. thanks... 请支持我..谢谢...

<!DOC HTML>
<html>
<head>
<script type="text/javascript">
var messages = [
    'Messages for <em>kindergarden</em> class',
    'Message for <em>1st grade</em>',
    'Message for <em>2nd grade</em>',
    'Message for <em>3rd grade</em>',
    'Message for <em>4th grade</em>',
    'Message for <em>5th grade</em>',
    'Message for <em>6th grade</em>'
];

var msgPtr = 0;
function nextMsg(direction) {
  msgPtr = msgPtr + direction;
  if (msgPtr < 0) { msgPtr = messages.length-1; }
  if (msgPtr > messages.length-1) { msgPtr = 0; }
  document.getElementById('msg').innerHTML = messages[msgPtr];
}
window.onload = function () {
  nextMsg(0);
}
</script>

<style type="text/css">
em { color:orange; }
#msg { 
font-family:monospace; 
background-color:yellow;
font-size:1em;  
width:890px; 
border:1px dotted red; 
overflow:hidden;}
</style>

</head>
<body>
<div id="msg"></div><br>
<button onclick="nextMsg(-1)">&lt;</button>
<button onclick="nextMsg(-1)">||</button>
<button onclick="nextMsg(1)">&gt;</button>
</body>
</html>

One solution would be to call nextMsg() recursively like 一种解决方案是像这样递归调用nextMsg()

function nextMsg(direction){
    //snip
    nextMsg(direction);
}

I wrote a very quick proof of concept with jQuery that should be rewritten in a cleaner way: http://jsfiddle.net/5NUPn/5/ 我用jQuery写了一个非常快速的概念证明,应该以一种更简洁的方式重写它: http : //jsfiddle.net/5NUPn/5/

Now for displaying both your message and buttons on the same line you need to use float. 现在,为了在同一行上显示消息和按钮,您需要使用float。

#msg { float: left; }

Using 'setInterval(code,millisec)' function. 使用'setInterval(code,millisec)'函数。 So your code should look like something like this: 因此,您的代码应如下所示:

var msgPtr =0;
var set_interval_id;

function pause() {
    clearInterval(set_interval_id)
}

function play() {
    set_interval_id = setInterval(function() {
        nextMsg(msgPtr);
        msgPtr++;
    }, 2000 /* how often to execute the code (2 sec) */);
}

window.onload = function () {
  play();
}

...

<button onclick="nextMsg(-1)">&lt;</button>
<button onclick="pause()">||</button>
<button onclick="nextMsg(1)">&gt;</button>

....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我按下播放上一个或播放下一个按钮时我的播放暂停按钮会移动(HTML、CSS、JS) - Why is my play-pause-button moving when i press the play-previous or play-next button (HTML, CSS, JS) jQuery字幕与下一个上一个和停止暂停/恢复按钮 - jquery marquee with Next Previous and Stop Pause / Resume Button JS/HTML 调查页面的下一个和上一个按钮 - Next and Previous button for JS/HTML survey page 纯JS中表格元素的下一个/上一个按钮 - Next/Previous button for table elements in pure JS Vanilla JS轮播下一个/上一个按钮杀死鼠标悬停事件暂停 - Vanilla JS carousel next/prev button killing mouseover event pause 如何检测键盘按键上的点击事件:Electron Js 上的播放/暂停(▶/❚❚)、下一个和上一个 - how to detect the click event on the keyboard keys : Play/Pause (▶/❚❚), Next and Previous on Electron Js Next-Previous按钮可使用Bootstrap选项卡循环工作 - Next-Previous button to work in loop using Bootstrap Tab 如何使用上一个和下一个按钮循环浏览锚链接? - How to loop through anchor links using a previous and next button? Javascript 图片库:for 下一个和上一个按钮的循环无法正常工作 - Javascript image gallery : for loop for next and previous button is not working correctly 在循环中迭代上一个下一个 - iterating previous next in a loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM