【问题标题】:Loading gif placeholder on:hover在:hover 上加载 gif 占位符
【发布时间】:2014-10-02 02:25:28
【问题描述】:

jsFiddle: http://jsfiddle.net/90gkv1gw/

我有一个加载动画 gif on:hover 的图像。如何修改它以在动画图像需要时间加载时显示某种加载 gif 占位符?

示例:http://giphy.com

如果您将鼠标悬停在他们的图片上,您会短暂看到一个从左向右滑动的绿色动画,但一旦图片完全加载,它就再也不会显示了。我怎样才能做到这一点?

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    $('.loading').mouseover(function(e) {
            if (e.target == e.currentTarget){
    
                var self = $(this),
                    loading = self.find('.loadingText'),
                    image = $('<img>').attr('src','http://media4.giphy.com/media/fa0ZTUSQu7AjK/200w.gif');
    
                loading.show();
                image.load(function () { loading.hide(); });
    
                self.siblings('img').remove();
                self.before(image);
            }
    
        }).mouseout(function() {
            var self = $(this),
                src = self.siblings('img').attr('src').replace('200w.gif', '200w_s.gif');
            self.siblings('img').attr('src', src);
        });
    a{
                position:relative;
                width:200px;
                height:150px;
            }
            .loading{
                position:absolute;
                width:200px;
                height:150px;
                top:0;
                left:0;
                text-indent:10px;
    
            }
            a img{
                position: absolute;
            }
            .loading .loadingText{
                background:red;
                color:white;
                font-size:12px;
                line-height:15px;
                display:none;
            }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    <a href="#">
        <img src="http://media4.giphy.com/media/fa0ZTUSQu7AjK/200w_s.gif" width="200" />
        <div class="loading">
            <div class="loadingText">Loading</div>
        </div>
    </a>

    【讨论】:

    【解决方案2】:

    可以使用jQuery的加载功能

    $( "#myimage" ).load(function() {
    
    //Put your code here to stop the loading animation
    });
    

    您可以在这里阅读更多内容http://api.jquery.com/load-event/

    【讨论】:

      猜你喜欢
      • 2017-05-08
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-14
      • 2019-05-23
      • 2019-02-03
      相关资源
      最近更新 更多