很久以前的一个作品,最近客户说是要把以前做的首页的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 + "&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);";