I have a container div of other divs and I am trying to animate container divs position. Normally I can achieve this animating thing but I got a problem now. I am trying to animate this div to the center of the screen because there will be some other content( which I want to hide with the help of container div) behind the container div when it is animated to the center, and if it is not fixed at the center, the contents (which are behind the container div) becomes visible from left and/or right of the container.
when I set the container divs position as;
CSS:
#cont{ position:absolute; right: auto; left: auto; top: 130px;}
Container div appears at center and doesn't move when browser zoomed. This is the position that I want to have after animation.
Then I set the divs first position and the javascript animation like this;
CSS:
#cont{ position:absolute; right:20px; left: auto; top: 130px;}
This css works for me. Container's initial position is just perfect for me.
JS:
$('.open').on('click', function(){
$('#cont').animate({"right":"auto"},1000)});
But when I click on the '.open' element. Nothing happens.
Normally the JS codes works because I tried to change positions like that:
setting initial position in css like right:auto; and setting in JS like "right":"20px"
This is the fiddle that I have problem, not animating position:
http://jsfiddle.net/ctarimli/B9h2w/3/
This is the fiddle which I tried reverse (I set initial "right" and "left" as auto; and then changed in JS)
http://jsfiddle.net/ctarimli/B9h2w/5/
So, why the div is not animating the position in the first fiddle?
You can't animate to an auto value - how should the JS animate to it? Suppose you start from 20px, what values should it use to interpolate? 21..22..23.. or 19..18..17.. (it can't determine what the "target" value should be)
As you stated in your comment, you want it to be centered afterwards. Try giving the #cont a fixed width (like width: 150px;). Then animate to a right position of 50% (right: 50%) and correct the margin with an appropriate margin-right: -75px; like so:
$('.open').on('click', function() {
$('#cont').animate({
"right":"50%",
"marginRight":"-75px"
}, 1000)
});
See Fiddle: http://jsfiddle.net/B9h2w/17/
Related
I've a div that I placed on top of a background image. The div is clickeable so it brings a popup dialog. The background image has got dots on which the div circles are to be placed.
The problem:
It works fine when both the background image is left aligned and the div elements have "position:absolute; left:x; top:y" but my problem is when I try to center align the image the absolute-x and absolute-y of the div are not being in the spots they have to be in the image.
Also when i change the size of the window the location of the div square shifts. I tried both absolute and relative positioning.
What I have:
<style>
.containerdiv { position: relative; width:100%; }
.cornerimage { position: absolute; top: 100px; left: 130px; }
.circle {
border-radius: 50%;
display: inline-block;
margin-right: 20px;
background: none repeat scroll 0 0 #701470;
height: 24px;
width: 24px;
}
</style>
And my div's here:
<div class="containerdiv" align="center">
<!-- my background image -->
<img border="0" src='http://www.infokerala.org/sites/default/files/images/barry_boehms_spiral_model.jpg' alt="">
<!-- trying absolute positioning -->
<div class="circle" id="bar" style="top: 142px; left:875px; position: absolute;"></div>
<!-- trying relative positioning -->
<div class="circle" id="foo" style="top: -68px; right: 332px; left:-332px; position: relative;"></div>
<div>
As you can see in the image, as I resize the window, the absolute positioned div starts moving off of the image.
The "relative" positioned div disappears after some time. I'm not sure why?
All I want is the div circles to be in fixed positions on top of the image regardless of the window size or where in the page I embed the image or any resizing activity.
What I wanted:
How the div moves out of it's initial location:
Finally even the "relative" positioned div also disappears, after I slowly shrink the window size :(
I understand that "absolute" positioning is absolute position with respect to the window, but why how can I make the div's stick to the original spots I wanted them to be?
Instead of using a div have you considered using an image map? You can make it look the way you want and it's positioning is based on the image so it will stay in the correct position. You can also attach the clicks as you would to a dive.
I know that image maps are considered a bit Web 1.0 but I have seen them used very effectively in exactly this sort of situation to produce very dynamic results with the addition of JavaScript and JQuery. This JQuery literary works greate: ImageMapster
Where as the floating div's solution has always seemed a bit like forcing a square peg in a round hole.
Since you know the size of the background image, it's easy to calculate percentile offsets instead of absolute offsets.
Say you have an image of 500x500 pixels as background, and you want to position the circle at position 50x50. If you put it at left:50px;top:50px the image will remain at that position even if the parent container resizes. However if you put it at left:10%;top:10% its offsets will scale along with the positioning parent.
so when you position it absolute, it is going to stay there, and when your background image moves because of the window resize, it is going to go away from the top of it.(actualt the circle is staying in its x,y position, and it is the background that is moving)
your solution is to go with relative.
I put your code in jsfiddle and the relative works fine
works fine here, not sure why you said it disapears after sometime? maybe some JS removes it?
You will need to add an event that recalculates the absolute position of the div either on page resize and onload:
<script>
function recalc(){
var width, height;
width = $('.containerDiv').width();
height = $('.containerDiv').height();
width = width / 2;
height = height / 2;
$("#bar").css({"position":"absolute","top": width + "px","left": height + "px",});
$("#foo").css({"position":"absolute","top": width + "px","left": height + "px",});
}
$(window).load(function(){ recalc();});
$(window).resize(function () { recalc();});
</script>
I need an div that will be always at the bottom of the page, margin 172px at the left, and 383px at the right.
This div will have some images and text and left and right buttons. When you hover the mouse at the right button, for example, the content that was "invisible", after reaching the div's width limit, will start appearing from the right, sliding the content for the left.
I tried using position:fixed; bottom:0px, but I couldn't margin the div, and the width of it doesn't change when the screen size changes...
For example, this would be exactly what I want (the black div at the bottom):
If you know any jquery plugin that does what I want or if you know how to do something like this, please help me!
If you're using position: fixed, margin can not be applied. You can specify the left and right attributes though.
position: fixed;
right: 383px;
bottom: 0;
left: 172px;
I know it's not exactly what you're asking for, but you can then set the white-space and overflow attributes on that div to make it so that it will show a horizontal scrollbar.
white-space: nowrap;
overflow: auto;
The user would use the scrollbar on the bottom to move the content of the div. Here's an example: http://jsfiddle.net/rustyjeans/5nv84/
To use jQuery set overflow: hidden and add some functions that adjust the scrollLeft of the div, then add some controls that call those functions when they're hovered. Here's an example: http://jsfiddle.net/rustyjeans/FtSGn/
This shouldn't be too hard to do. You want a containing div that has the dimensions of the viewer. Then, have a div inside that one, with position absolute and dimensions that extend beyond the viewer in width. When the arrows are hovered over use jquery to change the "left" css property of the inner div. Did that help?
EDIT:
The outer div should have "position: relative;" to insure that the inner div is positioned relative to its margins.
I have an issue that only affect Chrome. Furthermore its only visible when the screen is at certain widths.
I've created a fiddle that can replicate the issue.
http://jsfiddle.net/T8LvA/63/
When you rollover the red box the width of the parent is animated to reveal more of the red box.
You may need to adjust the width of the html pane several times before you see the wobble,
Any thoughts on how best to resolve this?
Thanks
Use float:right instead of positioning it absolutely.
http://jsfiddle.net/T8LvA/70/
It happens because when you change the width, it extends to the right - then it's reflowed and moves back to the left to the correct position, which causes the wobble. Floating it to the right always keeps it there.
To clarify: you'll need to replace position: absolute width float: right on both #widget and .hidden for the correct result.
if you use postion you need use left and top, in this case it is useless.
Try fx you css in this way
#wrapper{
width: 100%; // was 600px
height: 100px;
margin: 0 auto;
//position: relative;
}
I'm currently coding a jQuery slideshow effect and need a bit of help.
I have all of the sideshow functionality working properly, my only problem is that I want to have my navigation arrows to be automatically positioned on either side of the slideshow box (960px, centered on the screen).
The end product should be something like Kriesi does here: http://www.kriesi.at/themes/upscale/
I've looked at his code, but can't quite figure it out. Any help would be appreciated!
Thanks!
I don't understand what you mean by "I can't quite figure out how to initially position them over the slideshow... If I do it in CSS, then it won't work on all screen resolutions."...?
If you position the arrows relative to the slideshow, there will be no issue. For example to place them at the top left and top right corners, include the following in your styles:
#slideshowcontainer{
width: 960px;
height: 400px;
position: relative;
}
#leftarrow{
position: absolute;
top: 0px;
left: -40px; /* position the arrow 40px to the left of the slideshow */
}
#rightarrow{
position: absolute;
top: 0px;
right: -40px; /* position the arrow 40px to the right of the slideshow */
}
Obviously you will need to adjust the values to suit, depending on the size of your arrows and where you want them etc
Arrows are situated in . That block is positioned as absolute with top value as 50% - 12px (margin-top: -12px);
Then, there is a list which contains images and other data and affect height of it's parent .
So, basically, in the code, when user clicks on an arrow, jQUery probabaly uses outerHeight() to get height of li elements in and then uses animate() to change height of the which affects height of the and that in it's turn smoothly changes position of the arrows.
Personally, i think it's a bad designing when arrows change it's position. Very annoying when you have to move mouse up and down every time you want to see the next slide.
First, here's is my rough example: http://demindu.com/sandbox/simple.html
What I'm trying to do:
Create a content div: let's say 400px tall and 700px wide, like the example. The content box has a margin of 50px in each direction. The content div should always be centered both vertically and horizontally, regardless of screen resolution. The black background should extend from the centered content area all the way to the right side of the screen, but not to the left.
The only way I can think of possibly doing this is something using window.innerWidth & window.innerHeight in JavaScript, but I don't know enough to know if this is even possible.
The amount of blank space above and below the middle section would need to be:
window.innerHeight - height of the div (in this example: 500px [400px box with two 50px margins]) / 2
The blank space to the left of the black bar would need to be:
window.innerWidth - width of the div (in this example: 800px [700px box with two 50px margins]) / 2
My question to you is: Is this possible in JavaScript? Is this possible somehow with pure CSS?
You can do this entirely in CSS with 4-point absolute positioning. You will need two elements:
The first item spans from the right of the screen to the center where the content is positioned. This element uses absolute positioning for the top, left, and right coordinates of the element (we can leave bottom unspecified as it's taken care of by the height.)
The second item is nested in the former. This item has a fixed width to ensure the content itself remains in the specified width you've chosen. We can also set the height and padding on this object and the parent will inherit it's height. Don't use margins to simulate padding - it can cause cross browser issues when you're just trying to do some positioning tricks as we are here.
So your HTML code would look something like this:
<div id="my_centered_design">
<div id="my_centered_design_content">
<p>This is just some example text.</p>
</div>
</div>
And you're CSS would look like this:
div#my_centered_design {
background: #000;
margin-left: -400px;
margin-top: -250px;
left: 50%;
position: absolute;
right: 0;
top: 50%;
}
div#my_centered_design_content {
background: #333;
height: 400px;
/* I think you actually want padding for
the effect you're trying to accomplish */
padding: 50px;
width: 700px;
}
Essentially this is the same trick as the Joe2Tutorial except we are applying additional positioning rules to adhere the centered element to the right side of the screen.
I think this pure css solution would suit you best: http://www.joe2torials.com/view_tutorial.php?view=37
A very quick google resulted in this piece of code.
this code does not align a div in the middle. what you actually for your own website is that you put the following div css
.main {
width: 140px;background-color: #252525; float: left;margin-top: 25px; }
inside a table that is aligned to be centered. so, basically you're using the table's centering feature to center your left floated div simply as a content. you're not doing anything through div or css for that matter. the piece of css code you offered doesn't not anything about centering a div in the middle.