dmx*_*dmx 1 html javascript css
我想在我的应用程序上水平滚动.有多个例子,但我找到了一个符合我需要的例子.但是,当我尝试它时,它只是不能正常工作.请看,告诉我问题是什么:
<!DOCTYPE html>
<html>
<head>
<style>
div.marquee {
white-space:no-wrap;
overflow:hidden;
}
div.marquee > div.marquee-text {
white-space:nowrap;
display:inline;
width:auto;
}
</style>
<script>
var marquee = $('div.marquee');
console.log(marquee);
marquee.each(function() {
var mar = $(this),indent = mar.width();
mar.marquee = function() {
indent--;
mar.css('text-indent',indent);
if (indent < -1 * mar.children('div.marquee-text').width()) {
indent = mar.width();
}
};
mar.data('interval',setInterval(mar.marquee,1000/60));
});
</script>
</head>
<body>
<div class='marquee'>
<div class='marquee-text'>
Testing this marquee function
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
你忘了在你的网站中包含jQuery.否则,它按预期工作(至少我认为如此).
$(document).ready(function() {
var marquee = $('div.marquee');
console.log(marquee);
marquee.each(function() {
var mar = $(this),indent = mar.width();
mar.marquee = function() {
indent--;
mar.css('text-indent',indent);
if (indent < -1 * mar.children('div.marquee-text').width()) {
indent = mar.width();
}
};
mar.data('interval',setInterval(mar.marquee,1000/60));
});
});Run Code Online (Sandbox Code Playgroud)
div.marquee {
white-space:no-wrap;
overflow:hidden;
}
div.marquee > div.marquee-text {
white-space:nowrap;
display:inline;
width:auto;
}Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='marquee'>
<div class='marquee-text'>
Testing this marquee function
</div>
</div>Run Code Online (Sandbox Code Playgroud)
编辑:添加$(document).ready()以确保将加载元素.