如何在输入占位符内制作打字轮播?
How can I make a typing carousel inside an input placeholder?
我希望我的输入占位符有一个输入轮播,类似于 this
我希望 dynamic writing 在我的 #myInput
占位符上
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input">
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
关于如何完成此操作的任何想法?谢谢
您 link 使用 CSS 样式来模仿打字插入符的示例,因此您不能完全那样做,但除此之外您可以使用完全相同的 javascript您不是在修改跨度的内部 html,而是在修改输入的 placeholder
属性。
let input = document.getElementById("myInput");
input.placeholder = 'my text';
显然,上面的代码静态设置了占位符,但您没有理由不能使用提供的示例用于旋转占位符文本的 setTimeout
刻度策略。
至于插入符号,我只使用 'pipe' 符号:|
。它并不完美,但恕我直言,已经足够接近了。
您真的只需要用您的输入元素和文本创建一个新的 TxtRotate
对象。
new TxtRotate(element, textArray, waitTimeMs);
然后,要在占位符中获取文本,只需在元素上设置占位符文本即可。
this.el.placeholder = this.txt;
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.placeholder = this.txt;
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) {
delta /= 2;
}
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
new TxtRotate(document.getElementById('myInput'), ["PariMaria", "GentianAnnas", "LinneaSteliana", "UlisesCristobal", "SimonidesLonny"], 2000);
};
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input">
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
你可以试试这个版本
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Raleway:200,100,400" rel="stylesheet" type="text/css" />
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script>
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.value =this.txt;
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
var element = document.getElementById('myInput');
var toRotate = element.getAttribute('data-rotate');
var period = element.getAttribute('data-period');
new TxtRotate(element, JSON.parse(toRotate), period);
}
</script>
</head>
<body>
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input"
data-period="2000"
data-rotate='[ "nerdy.", "simple.", "pure JS.", "pretty.", "fun!" ]'>
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
</body>
</html>
您可以重复使用 TxtRotate,修改输入的占位符 属性 并为文本添加插入符效果。
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.placeholder = this.txt + '|';
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
new TxtRotate(document.getElementById('myInput'), [ "nerdy.", "simple.", "pure JS.", "pretty.", "fun!" ], 2000);
};
我希望我的输入占位符有一个输入轮播,类似于 this
我希望 dynamic writing 在我的 #myInput
占位符上
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input">
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
关于如何完成此操作的任何想法?谢谢
您 link 使用 CSS 样式来模仿打字插入符的示例,因此您不能完全那样做,但除此之外您可以使用完全相同的 javascript您不是在修改跨度的内部 html,而是在修改输入的 placeholder
属性。
let input = document.getElementById("myInput");
input.placeholder = 'my text';
显然,上面的代码静态设置了占位符,但您没有理由不能使用提供的示例用于旋转占位符文本的 setTimeout
刻度策略。
至于插入符号,我只使用 'pipe' 符号:|
。它并不完美,但恕我直言,已经足够接近了。
您真的只需要用您的输入元素和文本创建一个新的 TxtRotate
对象。
new TxtRotate(element, textArray, waitTimeMs);
然后,要在占位符中获取文本,只需在元素上设置占位符文本即可。
this.el.placeholder = this.txt;
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.placeholder = this.txt;
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) {
delta /= 2;
}
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
new TxtRotate(document.getElementById('myInput'), ["PariMaria", "GentianAnnas", "LinneaSteliana", "UlisesCristobal", "SimonidesLonny"], 2000);
};
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input">
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
你可以试试这个版本
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Raleway:200,100,400" rel="stylesheet" type="text/css" />
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script>
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.value =this.txt;
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
var element = document.getElementById('myInput');
var toRotate = element.getAttribute('data-rotate');
var period = element.getAttribute('data-period');
new TxtRotate(element, JSON.parse(toRotate), period);
}
</script>
</head>
<body>
<div class="form-group">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<center><label class="heypal-label"><span id="heypal">HeyPal<span class="gray-lighter">.me/</span></span></label>
<input id="myInput" type="text" class="form-control conference-input"
data-period="2000"
data-rotate='[ "nerdy.", "simple.", "pure JS.", "pretty.", "fun!" ]'>
<button type="submit" class="btn btn-lg btn-danger btn-autosize">Go!</button></center>
</div>
</div>
</body>
</html>
您可以重复使用 TxtRotate,修改输入的占位符 属性 并为文本添加插入符效果。
var TxtRotate = function(el, toRotate, period) {
this.toRotate = toRotate;
this.el = el;
this.loopNum = 0;
this.period = parseInt(period, 10) || 2000;
this.txt = '';
this.tick();
this.isDeleting = false;
};
TxtRotate.prototype.tick = function() {
var i = this.loopNum % this.toRotate.length;
var fullTxt = this.toRotate[i];
if (this.isDeleting) {
this.txt = fullTxt.substring(0, this.txt.length - 1);
} else {
this.txt = fullTxt.substring(0, this.txt.length + 1);
}
this.el.placeholder = this.txt + '|';
var that = this;
var delta = 300 - Math.random() * 100;
if (this.isDeleting) { delta /= 2; }
if (!this.isDeleting && this.txt === fullTxt) {
delta = this.period;
this.isDeleting = true;
} else if (this.isDeleting && this.txt === '') {
this.isDeleting = false;
this.loopNum++;
delta = 500;
}
setTimeout(function() {
that.tick();
}, delta);
};
window.onload = function() {
new TxtRotate(document.getElementById('myInput'), [ "nerdy.", "simple.", "pure JS.", "pretty.", "fun!" ], 2000);
};