Can I change the play icon of embedded youtube videos?

前端 未结 1 1659
陌清茗
陌清茗 2020-12-29 13:02

Am I allowed to replace the original youtube play icon on embedded youtube videos? I would place my own icon above the youtube iframe to achieve that. I know that facebook d

1条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 13:57

    I'm not sure you can customise a YouTube embedded without using the YouTube JavaScript api.

    Heres how to do it with the api:

    Javascript

    //youtube script
    var tag = document.createElement('script');
    tag.src = "//www.youtube.com/iframe_api";
    var firstScriptTag = document.getElementsByTagName('script')[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
    
    var player;
    
    onYouTubeIframeAPIReady = function () {
        player = new YT.Player('player', {
            height: '244',
            width: '434',
            videoId: 'AkyQgpqRyBY',  // youtube video id
            playerVars: {
                'autoplay': 0,
                'rel': 0,
                'showinfo': 0
            },
            events: {
                'onStateChange': onPlayerStateChange
            }
        });
    }
    
    onPlayerStateChange = function (event) {
        if (event.data == YT.PlayerState.ENDED) {
            $('.start-video').fadeIn('normal');
        }
    }
    
    $(document).on('click', '.start-video', function () {
        $(this).fadeOut('normal');
        player.playVideo();
    });
    

    HTML

    CSS

    button {
        position: absolute;
        top: 106px;
        padding: 12px;
        left: 174px;
    }
    

    I've created a Jsfiddle with a working example.

    0 讨论(0)
提交回复
热议问题