Skip to content

CHICPRO

  • Life Log
  • Cycling Log
  • Photo Log
    • Portrait
    • Landscape
    • Flower
    • Etc
  • Coding Log
  • Information

jQuery 플러그인 fitVideo.js를 워드프레스에서 사용하기

2014-03-23 by 편리

가끔 포스트에 유튜브 동영상을 첨부하곤 하는데 오늘 모바일로 접속해서 확인해보니 사이즈가 변경되지 않는 것을 확인하고 이전에 만들었던 플러그인 fitVidoe.js 를 워드프레스에 적용하기로 했다. 우선 아래는 이전에 만들었던 fitVideo.js 의 소스코드이다.

(function($) {
    $.fn.fitVideo = function(option) {
        var selectors = [
            "iframe[src*='player.vimeo.com']",
            "iframe[src*='youtube.com']",
            "iframe[src*='youtube-nocookie.com']",
            "iframe[src*='naver.com']",
            "iframe[src*='daum.net']",
            "iframe[src*='pandora.tv']",
            "iframe[src*='nate.com']",
            "object",
            "embed"
        ];

        var cfg = {
            selectors : selectors.join(', ')
        };

        var $this = this;

        return $this.each(function() {
            var $videos = $(this).find(cfg.selectors);
            var wrap_width = $this.width();

            $videos.each(function() {
                if(this.tagName.toLowerCase() === 'embed' && $(this).parent('object').length)
                    return true;

                var next = false;
                if(this.tagName.toLowerCase() === 'object' || this.tagName.toLowerCase() === 'embed')
                    next = true;

                if($(this).data("aspect") == undefined && !$(this).next("span").length) {
                    var w = parseInt($(this).attr("width"));
                    var h = parseInt($(this).attr("height"));
                    var $el = $(this);

                    if(next == true) {
                        if(!$(this).next("span").length)
                            $(this).after("<span style=\"display:none;\"></span>");

                        $el = $(this).next("span");
                    }

                    $el
                       .data("width", w)
                       .data("aspect", (h / w));
                }

                var width, height;
                var el_width = $(this).data("width");
                var el_aspect = $(this).data("aspect");

                if(next == true) {
                    el_width = $(this).next("span").data("width");
                    el_aspect = $(this).next("span").data("aspect");
                }

                if(parseInt(el_width) > wrap_width) {
                    width = wrap_width;
                } else {
                    width = el_width;
                }

                height = parseInt(el_aspect * width);

                $(this)
                    .attr({
                        width: width,
                        height: height
                    });
            });
        });
    };
}(jQuery));

 WordPress 에서 적용하기 위해서는 아래와 같이 사용하면 된다. 테마의 functions.php 파일에서 fitVideo.js 파일을 추가해준다. fitVideo.js 파일은 테마의 js 폴더에 생성한 것으로 한다.

<?php
if ( is_singular() ) {
    wp_enqueue_script( 'fitVideo', get_template_directory_uri() .'/js/fitVideo.js', array( 'jquery' ), '1.0', true );
}
?>

is_singular() 함수를 이용해 포스트의 내용보기 일 때만 플러그인이 로드뢰도록 했다. 그런 다음 fitVideo.js 제일 하단에 아래 내용을 추가해준다. selector는 이 블로그 테마 기준이다.

jQuery( function($) {
    $("#content article.single-post-article").fitVideo();

    $(window).on("resize", function() {
        $("#content article.single-post-article").fitVideo();
    });
});

스크립트를 추가하고 모바일로 접속해보면 컨텐츠 영역의 크기 변화에 따라 동영상의 크기가 달라지는 것을 확인할 수 있다.

Post navigation

Previous Post:

구글 나우가 영화 보라네

Next Post:

RSS feed 에러

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recent Posts

  • SK 세븐모바일 유심 셀프교체
  • php 배열 연산에서 + 와 array_merge 의 차이
  • pcntl_fork 를 이용한 다중 프로세스 실행
  • 아이폰 단축어를 이용하여 주중 공휴일엔 알람 울리지 않게 하기
  • 구글 캘린더 전체일정 재동기화
  • OpenLiteSpeed 웹서버에 HTTP 인증 적용
  • OpenLiteSpeed 웹어드민 도메인 연결
  • WireGuard를 이용한 VPN 환경 구축
  • Ubuntu 22.04 서버에 OpenLiteSpeed 웹서버 세팅
  • 맥 vim 세팅

Recent Comments

  • 편리 on 업무관리용 그누보드 게시판 스킨
  • 임종섭 on 업무관리용 그누보드 게시판 스킨
  • 캐논 5D 펌웨어 | Dslr 펌웨어 업그레이드 방법 82 개의 베스트 답변 on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • Top 5 캐논 5D 펌웨어 Top 89 Best Answers on 캐논 EOS 30D 펌웨어 Ver 1.0.6 , EOS 5D 펌웨어 Ver 1.1.1
  • 편리 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 임팀장 on 워드프레스 애니메이션 gif 파일을 mp4로 변환하여 출력하기
  • 편리 on Notepad++ NppFTP 플러그인 수동 설치
  • paul-j on Notepad++ NppFTP 플러그인 수동 설치
  • YS on Windows 10 iCloud 사진 저장 폴더 변경
  • 편리 on Docker를 이용한 Centos7 + httpd + php 5.4 개발환경 구축

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org
© 2025 CHICPRO | Built using WordPress and SuperbThemes