【发布时间】:2013-04-24 13:41:00
【问题描述】:
我正在尝试在我的网站上添加新的幻灯片。除了“height = “80%””选项外,幻灯片有我需要的一切,即我希望幻灯片随浏览器缩放,因为新的网站设计有点像 android 应用程序;完全身临其境。
因为幻灯片本身没有这个选项,我正在创建一个 JavaScript 代码,它将每 2 秒检查一次文档/浏览器窗口的大小,并重新加载/调整幻灯片本身的大小,使其始终适合屏幕。但是,问题是javascript只会运行一次,onload,并且在我将某个代码字符串粘贴到脚本后不会调用“setTimeout”。
所以,问题在于 setTimeout 实际上停止了工作,所以在我包含以下代码字符串之后它之前已经工作过:
var thescript = document.createElement("script");
thescript.type = "text/javascript";
thescript.innerHTML="jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: '"+calcheight+"px', background: '#000000'});";
document.getElementById('galleryid').appendChild(thescript);
完整的javascript检查功能在这里:
function getDocSpecs() {
clearTimeout(t);
var D = Math.max(
Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
Math.max(document.body.clientHeight, document.documentElement.clientHeight));
var Le = Math.max(
Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
Math.max(document.body.clientWidth, document.documentElement.clientWidth));
calcheight = (0.80 * D);
alert(preheight + "_" + prewidth + "_" + D + "_" + Le + "_");
if (preheight != D || prewidth != Le) {
var thescript = document.createElement("script");
thescript.type = "text/javascript";
thescript.innerHTML = "jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: '" + calcheight + "px', background: '#000000'});";
document.getElementById('galleryid').appendChild(thescript);
}
preheight = D;
prewidth = Le;
t = setTimeout('getDocSpecs()', 2000);
}
这两个人好像不太喜欢:
var thescript = document.createElement("script");
thescript.type = "text/javascript";
thescript.innerHTML="jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: '"+calcheight+"px', background: '#000000'});";
document.getElementById('galleryid').appendChild(thescript);
和
t = setTimeout('getDocSpecs()', 2000);
我试图通过先加载幻灯片然后调用函数、添加点击激活文本、调用多个函数等来欺骗它。
【问题讨论】:
-
为什么您认为您需要创建一个
<script>元素,向其中添加代码,并将其附加到某处?只需执行代码。 -
..天啊...,我不知道从哪里开始...
-
为什么不用interval而不是timeout呢?或者事件更好,为什么不 window.onresize?只需添加一个油门,这样它就不会调用一千次。
-
如果您不将要评估的字符串传递给
window.setTimeout,而是传递函数本身,问题是否仍然存在;window.setTimeout(getDocSpecs, 2000);? -
为什么不用
jQuery.flashgallery('gallery/ArtGallery.swf', 'gallery/gallery.xml', {width: '100%', height: calcheight+'px', background: '#000000'});替换你的第一个sn-p?
标签: javascript jquery settimeout innerhtml createelement