Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在一段文本上使用JavaScript设置穿透效果的动画?_Javascript_Animation - Fatal编程技术网

如何在一段文本上使用JavaScript设置穿透效果的动画?

如何在一段文本上使用JavaScript设置穿透效果的动画?,javascript,animation,Javascript,Animation,我想尝试创建一种效果,当我触发一个事件时,一条动画线条穿过一段文本。效果应该在Java脚本中完成 有人能建议一些方法吗?我已经在一个页面上有了文本,我希望文本从左到右划过,就像画了一条线一样您可以在字符串的开头添加一个标记,然后迭代地将结尾的标记向字符串的末尾再移动一个字符,可能最好使用setTimeout() 沿着这些路线的东西(未测试): STRIKE_POS=1; 原始长度=“”; 功能启动行程(str){ 走向位置=1; 原始STR=STR; 罢工(); } 函数罢工(){ 变量str=

我想尝试创建一种效果,当我触发一个事件时,一条动画线条穿过一段文本。效果应该在Java脚本中完成

有人能建议一些方法吗?我已经在一个页面上有了文本,我希望文本从左到右划过,就像画了一条线一样

您可以在字符串的开头添加一个
标记,然后迭代地将结尾的
标记向字符串的末尾再移动一个字符,可能最好使用
setTimeout()

沿着这些路线的东西(未测试):

STRIKE_POS=1;
原始长度=“”;
功能启动行程(str){
走向位置=1;
原始STR=STR;
罢工();
}
函数罢工(){
变量str=''+原始str.substr(0,删除位置)+''
+原始街次级街(走向位置);
//用字符串做一些事情,比如在某处显示
STRIKE_POS++;
如果(罢工位置<原罢工长度){
setTimeout(“strike()”,200);//调整超时
}
}

使用jQuery,只需稍加调整即可:

正在使用的JS:

var _text = "";
$(document).ready(function() {
    _text = $("#myDiv").text();
    StrikeThrough(0);
});

function StrikeThrough(index) {
    if (index >= _text.length)
        return false;
    var sToStrike = _text.substr(0, index + 1);
    var sAfter = (index < (_text.length - 1)) ? _text.substr(index + 1, _text.length - index) : "";
    $("#myDiv").html("<strike>" + sToStrike + "</strike>" + sAfter);
    window.setTimeout(function() {
        StrikeThrough(index + 1);
    }, 100);
}
var_text=”“;
$(文档).ready(函数(){
_text=$(“#myDiv”).text();
删除线(0);
});
函数删除线(索引){
如果(索引>=\u text.length)
返回false;
var sToStrike=_text.substr(0,索引+1);
var sAfter=(索引<(_text.length-1))?_text.substr(索引+1,_text.length-index):“”;
$(“#myDiv”).html(“+sToStrike+”+sAfter);
setTimeout(函数(){
删除线(索引+1);
}, 100);
}
这将穿透
myDiv
文本,使该行显示动画


由于它不使用任何繁重的jQuery内容,因此可以很容易地将其转换为纯JavaScript,因此如果您不喜欢使用jQuery,我将编辑我的答案。

我会在带有删除线样式的文本之前创建一个空白的范围。然后,我将编写一个函数,从文本前面弹出第一个字符,并将其附加到span。然后,使用setTimeout()反复调用该函数,直到文本为空


您要求提供一个建议-代码将花费更长的时间:)

您可以使用jQuery设置背景图像的动画,该图像看起来像删除线。也许是这样的:

$(".someclass").animate({backgroundPosition: '0px 0px'})
<span class="someclass">Lorem ipsum</span>
这看起来可能比尝试做一些涉及
标记的事情要顺利一些。您的HTML标记如下所示:

$(".someclass").animate({backgroundPosition: '0px 0px'})
<span class="someclass">Lorem ipsum</span>

以下是一个适用于当前IE、Firefox和Chrome版本的基本实现:

<html>
    <head>
        <script type="text/javascript">
            window.gradualStrike = function(spanId, timeMillis) {
                var stepDuration;
                var strikeElem = document.getElementById(spanId);
                var strikeText = strikeElem.innerHTML.replace("<S>", "<s>").replace("</S>", "</s>");  //IE uppercases the tag
                var currentStrikePos = strikeText.indexOf("</s>");
                if (currentStrikePos < 0) {
                    currentStrikePos = 0;
                    stepDuration = timeMillis / strikeText.length;
                }
                else {
                    if (currentStrikePos + 3 == strikeText.length) {
                        //the '</s>' is at the end, we are done
                        return;
                    }
                    currentStrikePos -= 3; //account for the '<s>' tag
                    stepDuration = timeMillis / (strikeText.length - 7);  //account for '<s>' and '</s>'
                    strikeText = strikeText.replace("<s>", "").replace("</s>", ""); //strikeText.replace(/\<s\>/, "").replace(/\<\/s\>/, "");

                }
                currentStrikePos++;
                strikeText = "<s>" + strikeText.substring(0, currentStrikePos) + "</s>" + strikeText.substring(currentStrikePos);
                strikeElem.innerHTML = strikeText;
                setTimeout("gradualStrike('" + spanId + "', " + timeMillis + ");", stepDuration);
            };
        </script>
    </head>
    <body>
        <span id="someText" onclick="gradualStrike('someText', 1000); this.onclick=function(){return;};">Click to strike...</span>
    </body>
</html>

window.gradualStrike=函数(spanId,timeMillis){
var阶跃持续时间;
var strikeElem=document.getElementById(spanId);
var stricketext=strickelem.innerHTML.replace(“,”).replace(“,”);//即标记的大写字母
var currentstrickepos=stricketext.indexOf(“”);
如果(当前罢工次数<0){
currentstrickepos=0;
stepDuration=时间毫秒/删除文本长度;
}
否则{
if(currentStrikePos+3==strikeText.length){
//“”结束了,我们结束了
返回;
}
currentStrikePos-=3;//用于“”标记的帐户
stepDuration=timeMillis/(strikeText.length-7);//说明“”和“”
stroketext=stroketext.replace(“,”).replace(“,”);//stroketext.replace(//\/,”).replace(//\/,”);
}
currentStrikePos++;
strikeText=“”+strikeText.子字符串(0,currentStrikePos)+“+strikeText.子字符串(currentStrikePos);
strickeelem.innerHTML=strickeText;
setTimeout(“渐变色(“+spanId+”,“+timeMillis+”;”,步长持续时间);
};
点击罢工。。。

  • 使用正则表达式遍历每个字符(除了
    \n
    ),并递归地使用回调函数将每个匹配的字符包装成
    span
  • 选择所有这些
    span
    元素,然后使用
    setInterval()
    遍历它们,通过
    span
    style
    对象添加
    style=“text-decoration:line-through
  • 当我们遍历每个
    span
    时停止

使用
innerHTML
的缺点是,当您序列化HTML时,您会丢失所有事件等。在上面的小提琴中,
strong
元素仍然可以单击(您将单击
span
,它将冒泡到父元素).

刚从谷歌来到这里,最后编写了我自己的简单小函数。我就是这样做的:

function drawLineOnClick() {

    //add or prepend div to the top of the div holding your text
    $("#IdOfElementHoldingTheText").prepend('<div id="lineThrough"></div>');
    var WidthStrikeThrEl = $("#IdOfElementHoldingTheText").width();

    $("#lineThrough").animate({width: WidthStrikeThrEl},1000, function() {

        //when line has been drawn, apply CSS line-through and remove line
        $("#IdOfElementHoldingTheText").attr('class', 'lineThrCssClass');
        $("#lineThrough").remove();
    });
}


#lineThrough {
  position:absolute;
  top:23px; //centering line over text to simulate actual line through
  width:0px;
  height:1px;
  background:#444; //color of line through
}

.lineThrCssClass {
    color:#444;
    text-decoration:line-through;
}
函数drawLineOnClick(){
//将div添加到保存文本的div顶部或在其前面加上前缀
$(“#IdOfElementHoldingTheText”)。前缀(“”);
var widthstrickethrel=$(“#idofelementtholdingthetext”).width();
$(“#lineThrough”).animate({width:widthstrickethrel},1000,function(){
//绘制线条后,通过应用CSS线条并删除线条
$(“#idofelementtholdingthetext”).attr('class','lineThrCssClass');
$(“#lineThrough”).remove();
});
}
#线穿过{
位置:绝对位置;
top:23px;//将线居中于文本上方,以模拟实际线通过
宽度:0px;
高度:1px;
背景:#444;//穿过的线条的颜色
}
.lineThrCssClass{
颜色:#444;
文字装饰:线条贯通;
}

您使用的是什么标记?香草JavaScript,还是使用库(jQuery、MooTools、Glow、Scriptaculous..)?目前未承诺使用任何框架。尽管将其与dojo一起使用会很好。这很酷,但最好不要使用序列化HTML
function drawLineOnClick() {

    //add or prepend div to the top of the div holding your text
    $("#IdOfElementHoldingTheText").prepend('<div id="lineThrough"></div>');
    var WidthStrikeThrEl = $("#IdOfElementHoldingTheText").width();

    $("#lineThrough").animate({width: WidthStrikeThrEl},1000, function() {

        //when line has been drawn, apply CSS line-through and remove line
        $("#IdOfElementHoldingTheText").attr('class', 'lineThrCssClass');
        $("#lineThrough").remove();
    });
}


#lineThrough {
  position:absolute;
  top:23px; //centering line over text to simulate actual line through
  width:0px;
  height:1px;
  background:#444; //color of line through
}

.lineThrCssClass {
    color:#444;
    text-decoration:line-through;
}