Skip to content

CHICPRO

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

iframe으로 첨부된 동영상 컨텐츠 영역에 맞게 보여주기

2013-11-27 by 편리

모바일 페이지 작업을 하다보면, 아니 꼭 모바일 페이지가 아니더라도 iframe 등을 이용해 동영상을 첨부했을 때 실제 컨텐츠 영역보다 영상의 width가 크다면 문제가 될 때가 있다. 이럴 때 사용하기 위해서 간단한 jQuery 플러그인을 작성했다.

(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));

사용법 : $(“#video”).fitVideo();

컨텐츠 영역에 있는 iframe 등의 동영상 엘리먼트의 기본 width 값과 가로 세로 비율을 data 메소드를 이용해 각각 저장하고 컨텐츠 영역의 넓이에 맞춰서 동영상 엘리먼트의 width, height 값을 계산해서 다시 적용해주는 것이다.

2013-11-20 18:15 추가 object를 이용해서 동영상을 첨부했을 때 크롬 등에서 동영상이 제대로 보이지 않는 오류 수정

Post navigation

Previous Post:

걷기에는 너무 추운 날씨

Next Post:

NGINX1.4.4 PHP5.5.6 MariaDB10.0.6 업데이트

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