javascript - How can i change the left margin of a div when the window is resized? - Stack Overflow

I want to place the red border div,on the left where the green border is,when the window is resized.Cu

I want to place the red border div,on the left where the green border is,when the window is resized.
Currently the div has the following css rules:

#twitter-2 { 
width: 332px; 
background-color: #7E7D7D; 
opacity: 0.6; 
margin-left:525px; 
margin-top:-142px; 
}

I solved the problem:

window.onresize = function(event) {
    var elem = document.getElementById("twitter-2");
    elem.setAttribute("style","margin: 20px 1px;");
}

I want to place the red border div,on the left where the green border is,when the window is resized.
Currently the div has the following css rules:

#twitter-2 { 
width: 332px; 
background-color: #7E7D7D; 
opacity: 0.6; 
margin-left:525px; 
margin-top:-142px; 
}

I solved the problem:

window.onresize = function(event) {
    var elem = document.getElementById("twitter-2");
    elem.setAttribute("style","margin: 20px 1px;");
}
Share Improve this question edited Mar 20, 2013 at 13:22 Iliescu Alex asked Mar 20, 2013 at 12:50 Iliescu AlexIliescu Alex 833 silver badges8 bronze badges 5
  • The red border is on the right, do you mean you want the green border to appear where the red border box is on the right? Also, post your HTML as well please. – Lowkase Commented Mar 20, 2013 at 12:53
  • @Lowkase I think he means when the page width is reduced, move the div highlighted in red to the position highlighted by the green border. – Antony Commented Mar 20, 2013 at 12:54
  • Sorry,i want to place the red border div under the 3 buttons,where the green border is. – Iliescu Alex Commented Mar 20, 2013 at 12:55
  • @lliescu Alex - Please post your HTML as well as any relevant CSS. – Lowkase Commented Mar 20, 2013 at 12:56
  • can you create a jsfiddle link of this so we can test it? – Dead Man Commented Mar 20, 2013 at 12:56
Add a ment  | 

3 Answers 3

Reset to default 4

you can use media queries

and test your site by re-sizing the window

adjust what you want on which device and always try to play with %'s not px's

Some media queries for you Thanks to Chris

/* Smartphones (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) {
/* Styles */
}

/* Smartphones (landscape) ----------- */
@media only screen 
and (min-width : 321px) {
/* Styles */
}

/* Smartphones (portrait) ----------- */
@media only screen 
and (max-width : 320px) {
/* Styles */
}

/* iPads (portrait and landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) {
/* Styles */


Like This 
#twitter-2 { 
margin-left:Some Value ; 
margin-top:Some Value ; 
}
    }

/* iPads (landscape) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) {
/* Styles */
}

/* iPads (portrait) ----------- */
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) {
/* Styles */
}

/* Desktops and laptops ----------- */
@media only screen 
and (min-width : 1224px) {
/* Styles */
}

/* Large screens ----------- */
@media only screen 
and (min-width : 1824px) {
/* Styles */
}

/* iPhone 4 ----------- */
@media
only screen and (-webkit-min-device-pixel-ratio : 1.5),
only screen and (min-device-pixel-ratio : 1.5) {
/* Styles */
}

Try it like

CSS

.red { 
    width: 332px; 
    background-color: #7E7D7D; 
    opacity: 0.6; 
    margin-left:525px; 
    margin-top:-142px; 
}

.green { 
    width: 332px; 
    background-color: #7E7D7D; 
    opacity: 0.6; 
    margin-left:25px; 
    margin-top:-342px; 
}

Suppose currently your twitter-2 div has class red

On window.resize function

$(window).resize(function(){
   $('#twitter-2').addClass('green').removeClass('red');
});

Also you can do it for different width as you required.

Read http://api.jquery./resize/

Just put the whole line on a container than float left and right, when the space is too small the box will go underneath naturally.

<div class="row">
    <div class="follow">keep in touch</div>
    <div class="tweet-update">bla bla</div>
</div>

.follow {float:left; margin-bottom:30px;}
.tweet-update {float:right}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744622611a4584437.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信