开发者

javascript horizontal center

开发者 https://www.devze.com 2022-12-20 13:46 出处:网络
Im trying to use java script t开发者_C百科o center a page horizontally. So far i got this: <body onload=\"scrollBy((document.body[\'scrollWidth\'] - 0) / 2, 0)\">

Im trying to use java script t开发者_C百科o center a page horizontally.

So far i got this:

<body onload="scrollBy((document.body['scrollWidth'] - 0) / 2, 0)">

It kinda works, but not very good. I have a large width flash component that I need centered when the page loads and also after any click on the internal buttons.

I was thinking of making a call from flash to a javascript that would scroll the page to an HTML anchor.

Something along the lines of:

 <script type="text/javascript">
 function pageMid() {
 window.location.href="#mid"
 }
 </script>

But the anchors don't align to middle.

Any ideas?

Thank you.


This worked:

<script type="text/javascript">
function pageMid() {
scrollTo(1000, 0);
}
</script>

 <body onload="pageMid();">

Now I just cant figure it out on how to call this from flash catalyst...


It sounds like you want to center the element vertically, not horizontally. If you know the size of the element you're trying to center, use CSS:

<style>
#myThing {
   height: 300px;
   width: 400px;
   position:absolute;
   top:50%;
   left:50%;
   margin-top:-150px;
   margin-left:-200px;
}
</style>

<div id="myThing"></div>


This works

$(function () {
    scrollTo(($(document).width() - $(window).width()) / 2, 0);
});

Edit:
I got 2 downvotes for this. Maybe because I did not specify that this solution requires jQuery?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号