【问题标题】:Fully expand this CSS inline-block div scan-line完全展开这个 CSS inline-block div 扫描线
【发布时间】:2013-03-10 13:43:00
【问题描述】:

我希望这个扫描线效果能够正常工作。从左到右显示文本。仿佛阴极射线正在将其燃烧到屏幕上的荧光粉中。

这个想法是在黑色的行上滑动,这些行有一个透明的尖端。 Here is a 80% working demo. 每行最右边的黑色.mask div 不会展开。必须的。

我试图将最右边的.mask div 与黑色背景作为内联块并使其全宽。我有点理解为什么请求不起作用(宽度:100% 将其他内联块推到下一行,这是正确的),但是必须有一种方法可以在不破坏 javascript 宽度的情况下获得这个完整的右侧.

.row {
        font-family:'Courier New',Courier,monospace;
        font-size:16px;
        display:block;
        height:auto;
        width:100%;
        min-width:20%;
        position:relative;
        margin-right:0px;
}

.mask {
        display:inline-block;
        width:auto; /* 100% does not work */
        background:black;
        white-space:pre;
}

【问题讨论】:

  • 你的 jsbin 中的 html 中是否有 js 与 css 混合?为了让我们的生活更轻松,请将 html 与 js 与 css 分开。我无法让您的代码在我自己的机器上运行
  • 好吧,当你已经有一个 CSS 文件时,有必要在 HTML 中使用样式块吗?

标签: javascript jquery css scanline


【解决方案1】:

这在 jsbin 上不起作用,因为它使用绝对定位(除非您查看全屏演示).. 但我还是提供了它供您将其复制/粘贴到您自己的浏览器中 http://jsbin.com/uteyik/12/.. 下面是更改亮点:

css:

.row {
 ..
  position:absolute;   /* changed to absolute */

}
.mask {
  ..
  width:100%;  /* changed to 100% */
  position:absolute;   /*changed to absolute */
}

javascript:

jQuery(document).ready(function() {
    function fill_box(rows) {
        var rowHeight = getSampleRowHeight();
        var thisRowHeight = 0;
        for(var i = 0; i < rows; i += 1) {
            $('.box').append('<div class="row" style="top: '+thisRowHeight+'"><div class="scan_cursor i1"> </div><div class="scan_cursor i2"> </div><div class="scan_cursor i3"> </div><div class="mask"> </div></div>');       
            thisRowHeight +=rowHeight;
        }   
    }

    fill_box(30);

    function tag_animate(el) {
        // animate the mask
        el.animate( {
            'margin-left' : '100%'
            }, 
            {
                complete : function () {        
                    tag_animate(el.parent().next().find('.mask'));
                }
            } 
        );
        // animate the stripes
        el.siblings().animate( {
            'margin-left': '100%'
            }, 
            {
               complete : function () {     
                   el.siblings().hide();
               }
            }
        );      
    }    

    tag_animate($('.box').find('.row').eq(0).find('.mask'));

    function getSampleRowHeight() {
        // get sample row height, append to dom to calculate
        $('.box').append('<div class="row" style="display: hidden"> <div class="scan_cursor i1"> </div></div>');
        var rowHeight = $('.row').height();
        $('.box').find('.row').remove();
        return rowHeight;
    }    
 });

解释:我首先创建一个虚拟行并计算它的高度。然后我创建具有position: absolute 的行,并使用虚拟行高x 行号从顶部定位它。 想法是我想让所有东西都具有绝对定位,这样当遮罩为 100% 时,它就不会推动下面的条纹,而且行必须是绝对的,因为当我让它的内容消失时,我不希望下面的行跳起来。

奖励:看到它以相反的方式工作(即文本消失):http://jsbin.com/uteyik/9这是我最初的(并且不正确的)答案

【讨论】:

  • 我喜欢这样。但是,我希望效果是“显示”而不是“隐藏”文本。
  • 在所有这些工作之后哈哈!没关系,我 shudda 澄清了.. 回到绘图板我猜:p
  • 是的,正如我在回答中提到的那样..只需将我的代码从 jsbin 复制并直接粘贴到您的浏览器中.. 由于某种原因 jsbin 搞砸了(不要忘记将 javascript 包装在 jsbin 中使用 jQuery(document).ready(function() {.. 或者只是从上面复制我的 javascript 代码)
  • 它在jsbin中工作!它运作良好,除了遮罩高度偏离线 1px,但这个想法有效。谢谢。获奖。
  • 我的道歉@CrisStringfellow,坦率地说,我们上班迟到了,我不得不跑步..(我会在最近的机会编辑)..这是一个很好的问题,尝试解决很有趣.. 以后每当你有类似的问题,请让我知道! :) 并感谢正确答案奖 :)
猜你喜欢
  • 1970-01-01
  • 2014-11-24
  • 2013-05-22
  • 2021-07-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 2013-05-18
相关资源
最近更新 更多