culturenet

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  20 随笔 :: 2 文章 :: 136 评论 :: 17 引用

很久以前的一个作品,最近客户说是要把以前做的首页的javascript做的一个图片轮换的效果换成类似sina体育的那个图片轮换的效果。

     于是开工,网上一搜索,就有一个,摘录如下:

第一步:先把他的flash图片播放器下载下来。http://news.ccidnet.com/images/swf/pixviewer.swf

第二步:把以下代码放到你要显示的地方。
不过有几处需要修改的地方。pixviewer.swf放的路径,图片地址,链接的网页

<script type="text/javascript">
<!--

var focus_width=260
var focus_height=210
var swf_height = focus_height

var pics='pic/1.jpg|pic/2.jpg|pic/3.jpg|pic/4.jpg'//图片地址(相对路径)

var links='1.htm|2.htm|3.htm|4.htm'//(链接的网页)

document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+ focus_width +'" height="'+ swf_height +'">');
document.write('<param name="allowScriptAccess" value="sameDomain"><param name="movie" value="pixviewer.swf"><param name="quality" value="high"><param name="bgcolor" value="#F0F0F0">');
document.write('<param name="menu" value="false"><param name=wmode value="opaque">');
document.write('<param name="FlashVars" value="pics='+pics+'&links='+links+'&borderwidth='+focus_width+'&borderheight='+focus_height+'">');
document.write('<embed src="pixviewer.swf" wmode="opaque" FlashVars="pics='+pics+'&links='+links+'&borderwidth='+focus_width+'&borderheight='+focus_height+'" menu="false" bgcolor="#F0F0F0" quality="high" width="'+ focus_width +'" height="'+ swf_height +'" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');document.write('</object>');

//-->
</script>

于是实现我的代码:

if (!Page.IsPostBack)
        {

            TList<Dlbj_xw> myxw = DataRepository.Dlbj_xwProvider.GetTopN(5, "xw_id desc", "xw_pic_path is not null  and xw_ssfl='" + (int)MyEnum_Dlbj_xw_enum.要闻 + "'");
            string htmlstring=@"<SCRIPT type=text/javascript>
<!--  
    var focus_width=300
    var focus_height=225
    var text_height=25
    var swf_height = focus_height+text_height

    var pics='";           

            string pics="";
            string texts="";
            string urls="";
            foreach(Dlbj_xw thexw in myxw)
            {

                char[] charSeparators = new char[] { '/' };
                pics += "uploadfiles/xw/images/" + thexw.Xw_pic_path.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries)[0] + "|";
                texts += CutString.cutstring(thexw.Xw_title,25) + "|";
                urls += "view_news_inde.aspx?id=" + thexw.Xw_id + "&amp;xwlb=1|";

            }
            pics=pics.Substring(0,pics.Length-1);
            texts=texts.Substring(0,texts.Length-1);
            urls=urls.Substring(0,urls.Length-1);

            htmlstring+=pics+"'\n";
            htmlstring+="var links='"+ urls +"'\n";
             htmlstring+="var texts='"+ texts +"'\n";
            htmlstring+="document.write('<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" name=\"flashpic\" id=\"flashpic\" width=\"'+ focus_width +'\" height=\"'+ swf_height +'\">');";

     htmlstring+="document.write('<param name=\"allowScriptAccess\" value=\"sameDomain\"><param name=\"movie\" value=\"pixviewer.swf\"><param name=\"quality\" value=\"high\"><param name=\"bgcolor\" value=\"#DADADA\">');";
     htmlstring+="document.write('<param name=\"menu\" value=\"false\"><param name=wmode value=\"opaque\">');";
     htmlstring+="document.write('<param name=\"FlashVars\" value=\"pics='+pics+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\">');";
     htmlstring += "document.write('<embed src=\"pixviewer.swf\" wmode=\"opaque\" FlashVars=\"pics='+pics+'&links='+links+'&texts='+texts+'&borderwidth='+focus_width+'&borderheight='+focus_height+'&textheight='+text_height+'\" menu=\"false\" bgcolor=\"#DADADA\" quality=\"high\" width=\"'+ focus_width +'\" height=\"'+ swf_height +'\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />'); "; 
     htmlstring+="document.write('</object>');";
     htmlstring+="//-->";
     htmlstring+="</SCRIPT>";
     Literal1.Text = htmlstring;

        }

测试一下,发现效果还不错。点了一下鼠标,发现链接有问题,分析一下,主要是通过FlashVars传递参数的时候 变量 links中的链接地址中有"&"符号,于是出现了一些偏差。于是尝试着其他的传值方式。

发现,可以在 src="xxx.swf?links="中采用如次的方式,但是传入的也是在“&”的时候产生了错误。

最后就修改代码,使用javascript向flash中传递参数。

代码修改后加入如下代码就ok:

htmlstring+="window.document.flashpic.SetVariable(\"links\", links);";

posted on 2007-06-27 20:45 culturenet 阅读(723) 评论(1) 编辑 收藏

评论

#1楼 2007-07-31 12:27 culturenet
今天看到了一个更好的方式: 用%26替换掉“&”。


相关地址: http://www.permadi.com/tutorial/flashVars/


Both tags serve the same purpose, to specify a Flash movie. However, most Netscape browsers only understand EMBED tags, while Internet Explorer only understand OBJECT tags. Whenever you specify a Flash movie, you should use both. So, when using FlashVars, there are also two slightly different syntax, one for the OBJECT tag, and the other one for EMBED tag.

For the OBJECT tag, the syntax is as follows: <PARAM NAME FlashVars VALUE=[variable1=value1&variable2=value2&variable3=value3...]>



The param tag must be nested within the OBJECT tag.

For the EMBED tag, it is as follows: <EMBED .... FlashVars=[variable1=value1&variable2=value2&variable3=value3...] ...>



Each variable variable and value pair is separated by a '=' sign. A '&' sign separates the each pair.
So for example, if I have two variables, userName and score, the tags would look like this: <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://macromedia.com/cabs/swflash.cab#version=6,0,0,0"
ID=flaMovie WIDTH=250 HEIGHT=250>
<PARAM NAME=movie VALUE="flaMovie.swf">
<PARAM NAME=FlashVars VALUE="userName=permadi&score=80">
<PARAM NAME=quality VALUE=medium>
<PARAM NAME=bgcolor VALUE=#99CC33>
<EMBED src="flaMovie.swf"
FlashVars="userName=permadi&score=80"
bgcolor=#99CC33 WIDTH=250 HEIGHT=250
TYPE="application/x-shockwave-flash">
</EMBED>
</OBJECT>



Several things that should be noted about variable names:

Use only letters, underline, and numbers. There should be no reason why symbols such as '$' or '#' must be used as variable names.
Do not start a name with a number (for example: 1message is an invalid variable name because it starts with a number; whereas message1 is a valid variable name).
Flash will certainly reject or get confused if you use a variable name that starts with a number or other special characters - except underlines (ie: _message, and _1message are valid names).
A variable name should not contain any <space> character (ie: my message is an invalid name, my_message is a valid name).
And for variable values, there are also rules:

Characters needs to be URL encoded (see: Introduction to URL Encoding). This means that special characters, such as =, &, <SPACE>, + need to be substituted with their URL encoded form. For example: name=John Doe. Here, the <space> between John and Doe needs to be encoded with + sign, so you need to pass it as name=John+Doe or name=John%20Doe. (Flash doesn't seem to enforce this, but you should follow the proper way.) Search for URL encoding on the net for more details on this subject.
If you need to include an & as part of a value, such as in ingredients=Beef&Pork, then you need to encode the & like this: ingredients=Beef%26Pork. The %26 is an encoded form of the & sign. The number 26 is the hexadecimal code of the character &. If you need to know the encoding value of a particular character, consult an ASCII table. There are other characters that need to be encoded, such as <, >, /, ?.

 回复 引用