缩略图点击时弹出iFrame弹出窗口

时间:2016-05-18 14:26:42

标签: javascript jquery html css

我有这个脚本从Twitch API获取缩略图,并且点击缩略图它会打开一个带有实际流的iFrame。如何在屏幕中间弹出iFrame弹出窗口,其他一切变得更暗?

$('.games > div').click((e) => {
    var gameDiv = $(this); 
    $('#twitch').children().fadeOut(500).promise().then(function() {
        $('#twitch').empty();

            var gameName = $(e.target).text().replace(' ', '%20');
            console.log(gameName);
            var twitchApi = "https://api.twitch.tv/kraken/streams?game=" +gameName; 
            $.getJSON(twitchApi, function(json) {
                setData(json.streams)
        });

        function setData(twitchData) {
            var i = 0;
            var j = twitchData.length > (i + 9) ? (i + 9) : twitchData.length; for
                (; i < j; i++) {
                var streamGame = twitchData[i].game;
                var streamThumb = twitchData[i].preview.medium;
                var streamVideo = twitchData[i].channel.name;
                var img = $('<img style="width: 250px; height: 250px;" src="' + streamThumb + '"/>');
                $('#twitch').append(img);
                img.click(function() {
                    $('#twitch iframe').remove()
                    $('#twitchframe').append( '<iframe frameborder= src="http://player.twitch.tv/?channel=' + streamVideo + '"></iframe>');
                });
            }
        }
            $('#load').click(function() {
        setData();
    }); 
            });

});

1 个答案:

答案 0 :(得分:2)

此代码适用于我

<div class="overlay">
    <iframe src="iframe-src-url"></iframe>
</div>
<style>
    .overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.74); /* semi-transparent background */
        text-align: center;
        display: none; /* hide this by default */
    }

    .overlay iframe {
        margin: 30px auto;
    }
</style>

Javascript代码

    function setData(twitchData) {
        var i = 0;
        var j = twitchData.length > (i + 9) ? (i + 9) : twitchData.length; for
            (; i < j; i++) {
            var streamGame = twitchData[i].game;
            var streamThumb = twitchData[i].preview.medium;
            var streamVideo = twitchData[i].channel.name;
            var img = $('<img style="width: 250px; height: 250px;" src="' + streamThumb + '"/>');
            $('#twitch').append(img);
            img.click(function() {
                $('#twitch iframe').remove()

                // append it to the overlay then show
                $('.overlay').append( '<iframe frameborder= src="http://player.twitch.tv/?channel=' + streamVideo + '"></iframe>').show();
            });
        }
    }

希望这适合你

&#13;
&#13;
$('h1').click(function() {
    $('.overlay').append('<iframe src="iframe-src-url"></iframe>').show();
});
&#13;
    .overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.74); /* semi-transparent background */
        text-align: center;
        display: none; /* hide the overlay by default */
    }

    .overlay iframe {
        margin: 30px auto;
    }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Sample text</h1>
<div class="overlay"></div>
&#13;
&#13;
&#13;