EonerCMS-CMS (7) for a desktop-like system)

Source: Internet
Author: User

Intelligently modify the location of a window

During the test, I found that after adjusting the browser size, the window close to the browser edge was directly hidden, such:

The cause of this problem is also very simple, because the top and left values of the window are written to death, when the width and height of the browser is smaller than the top and left values of the window, it will inevitably be hidden. The solution is to modify the top and left of the window while adjusting the browser size. But how much does it need to be modified?

After your own consideration and the guidance of others, the final solution is to change proportionally, that is to say, scale the ratio of the distance from the current window to the left, top, and bottom of the window to the size of the whole window. It may be a bit confusing. Let's look at the modification formula:

  Top = top margin of the browser zoom window/(height before browser zoom-height of the window before browser zoom) * (height after browser zoom-height of the window after browser zoom );

  Left = the left margin of the window before the browser zoom/(the width before the browser zoom-the width of the window before the browser zoom) * (the width after the browser zoom-the width of the window after the browser zoom );

In the formula, I subtract the width and height of the window from the width and height of the browser. The reason is that no matter how the browser scales, the width and height of the window will never change. If the width and height of the window are not subtracted, it cannot be scaled proportionally.

The final effect is as follows:

You should have seen the demo. Try zooming the browser to see the effect.

I'm a demo. Read the article and read it again.

Demo source code, js

$().ready(function(){var dfw = $(window).width() - $("#div1").width();var dfh = $(window).height() - $("#div1").height();var dl = $("#div1").offset().left;var dt = $("#div1").offset().top;$(window).bind("resize",function(){var ctw = $(window).width() - $("#div1").width();var cth = $(window).height() - $("#div1").height();$("#div1").css({"left":(dl/dfw*ctw)+"px","top":(dt/dfh*cth)+"px"});});});

Html

<Div id = "div1" style = "width: 300px; height: 30px; line-height: 30px; text-align: center; padding: 5px; background: red; position: absolute; top: 100px; left: 100px "> I am a demo. I will read the article again. </div>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.