Change CSS style from javascript?
I would prefer not to use jquery just for simplicity. I have three websites which one page cycles through. I want the webpages to be scaled each by a different scalar value. I tried applying a class to each page but with a switch statement but it didn't work and it would only stay on the third style. I don't care about efficiency, it's only going to be three pages this scrolls through so it can be hardcoded. Thanks
<style>
#wrap { width: 1390px; height: 690px; padding: 0; overflow: hidden; }
#frame.first { width: 1390px; height: 690px; border: 0px solid black; }
#frame.first { zoom: 2; -moz-transform: scale(2); -moz-transform-origin: 0 0; }
#frame.second { width: 1395px; height: 695px; border: 0px soli开发者_StackOverflowd black; }
#frame.second { zoom: 4; -moz-transform: scale(1); -moz-transform-origin: 0 0; }
#frame.third { width: 1395px; height: 695px; border: 0px solid black; }
#frame.third { zoom: .5; -moz-transform: scale(1); -moz-transform-origin: 0 0; }
</style>
<script type="text/javascript">
var frames = Array('http://www.google.com, 5,
'http://www.yahoo.com', 5,
'http://www.ebay.com', 5);
var i = 0, len = frames.length;
function ChangeSrc()
{
if (i >= len) { i = 0; }
switch(i)
{
case 0:
document.getElementById('frame').className = 'first';
document.getElementById('frame').className
case 1:
document.getElementById('frame').className = 'second';
document.getElementById('frame').className
case 2:
document.getElementById('frame').className = 'third';
document.getElementById('frame').className
}
document.getElementById('frame').src = frames[i++];
setTimeout('ChangeSrc()', (frames[i++]*1000));
}
window.onload = ChangeSrc;
</script>
</head>
<body>
<div id="wrap">
<iframe src="" class="" id="frame" scrolling="no" frameborder="0"></iframe>
</div>
</body>
</html>
You need break statements to prevent the switch from falling through to the next statement.
switch(i)
{
case 0:
document.getElementById('frame').className = 'first';
break;
case 1:
document.getElementById('frame').className = 'second';
break;
case 2:
document.getElementById('frame').className = 'third';
break;
}
Your switch statement is failing because it lacks a break
statement at the end of each case. Without break
, the proper case and any cases below it will execute. That is why "only the third one works"
精彩评论