Javascript - 从tumblr滚动随机图像

时间:2015-08-21 23:01:04

标签: javascript

所以,我试图从某人的Tumblr推出一张随机图像,但它并没有正常工作。问题是,有时它没有选择照片而且背景保持黑色(它在我的.css文件中定义),如何修复?我在这里借用了其他一些问题的代码。提前谢谢。

    <script>
    $(document).ready(function () {
    var link = "http://api.tumblr.com/v2/blog/www.30000fps.com/posts?";
    $.ajax({
        type: "GET",
        url: link,
        dataType: "jsonp",
        data: {
            api_key: "ClAjag2DrKwJhbFY1aAAwqBUxEFBEOBwc7AzMwXlcaNlp3gZte"
        }
    }).done(function (data) {

        var url = $.rand(data.response.posts);
        url = url.photos[0].original_size.url;
        console.log(url);
        console.log($.rand(data.response.posts));
        $('DIV.background').css('background-image', 'url(' + url + ')');;

        });
    });

(function ($) {
            $.rand = function (arg) {
                if ($.isArray(arg)) {
                    return arg[$.rand(arg.length)];
                } else if (typeof arg == "number") {
                    return Math.floor(Math.random() * arg);
                }
            };
        })(jQuery);

    </script>

1 个答案:

答案 0 :(得分:0)

我发现有时您的随机化结果是视频而不是照片,因此请尝试使用以下代码仅对照片进行随机设置:

# Check if the stage is in or before the supplied stage (stage_to_check).
def in_or_before_stage?(stage_to_check)
  if stage_to_check.present? && self.stage.present?
    STAGES_IN_ORDER.reverse.lazy.drop_while { |stg| stg != stage_to_check }.include?(self.stage)
  else
    false
  end
end