【问题标题】:Dynamically resizing an embeded video with jQuery?使用 jQuery 动态调整嵌入视频的大小?
【发布时间】:2011-01-10 17:57:39
【问题描述】:

我尝试了几种方法,但似乎没有任何效果。我想做的是让我的用户使用给定的嵌入代码发布视频(示例):

<object id="cnbcplayer" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="380" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
 <param name="type" value="application/x-shockwave-flash" />
 <param name="allowfullscreen" value="true" />
 <param name="allowscriptaccess" value="always" />
 <param name="quality" value="best" />
 <param name="scale" value="noscale" />
 <param name="wmode" value="transparent" />
 <param name="bgcolor" value="#000000" />
 <param name="salign" value="lt" />
 <param name="src" value="http://plus.cnbc.com/rssvideosearch/action/player/id/1398301408/code/cnbcplayershare" />
 <param name="name" value="cnbcplayer" />
 <embed id="cnbcplayer" 
        type="application/x-shockwave-flash" 
        width="400" 
        height="380" 
        src="http://plus.cnbc.com/rssvideosearch/action/player/id/1398301408/code/cnbcplayershare" 
        name="cnbcplayer" 
        salign="lt" 
        bgcolor="#000000" 
        wmode="transparent" 
        scale="noscale" quality="best" 
        allowscriptaccess="always" allowfullscreen="true">  
 </embed>

但是,我想将其调整为 300 像素宽。我试过用 jQuery 更新属性,但是没有用。关于什么可以解决这个问题的任何想法?

谢谢

【问题讨论】:

  • 能否将代码重新格式化为多行,好吗?读起来有点难。
  • 重新格式化 - 仍然是基本的 Flash 电影嵌入 :)

标签: jquery flash video embed


【解决方案1】:

我写了这个函数:

/**
 * resize_embedded resizes embedded videos like youtube videos;
 * Examples: 
 *  - resize_embedded($('youtube_div'), 300, 200);
 *  - Fiddle: http://jsfiddle.net/xjxVC/1
 *
 * Parameters:
 * @param jquery the element that contains the embedded media
 * @param number the new width
 * @param number the new height
 *
 */
function resize_embedded(){

    // Did we receive 3 correct parameters
    if(arguments.length === 3 && $(arguments[0]).length && isNumber(arguments[1]) && isNumber(arguments[2])) 
        ;
    else
        return 0;

    // Clone embedded element
    $c = $(arguments[0]).clone();

    // Resize clone (replace width/height of all children who have them) 
    $c
        .attr('width', arguments[1])
        .attr('height', arguments[2])
        .children().each(function(){     
            $(this).attr('width') && $(this).attr('width', arguments[1]);
            $(this).attr('height') && $(this).attr('height', arguments[2]);
    })

    // Replace target with clone
    $(arguments[0]).replaceWith($c);
}

function isNumber(n){
    return !isNaN(parseFloat(n)) && isFinite(n);
}

演示:http://jsfiddle.net/xjxVC/1

【讨论】:

  • 如果视频正在播放,您的函数将重新启动它。不是用户所期望的。
【解决方案2】:

尝试将objectwidth: "100%" 放入div 并调整该div 容器的大小。

【讨论】:

  • 这将起作用,但始终使视频宽度为 100%。我将它用作博客的侧边栏,所以我想在帖子中保持正确的纵横比,但在主页侧边栏上预览最新的视频。我尝试使用 jQuery 来更新属性,但它似乎不起作用: jQuery('#sideVideo').closest('object').attr('width','100%');我在 jQuery 方面遗漏了一些东西,但不确定是什么。
  • Darmen - 虽然这有效,但它实际上与 jQuery 相关 - 我想我不能使用“最接近”,而是必须指定我试图修改的子对象。
  • 嗯...试试Query('#sideVideo').closest('object').parent("#yourDiv").attr('width','500px'); 并将width: "100%" 指定为object
猜你喜欢
  • 1970-01-01
  • 2017-04-15
  • 1970-01-01
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 1970-01-01
相关资源
最近更新 更多