I'm trying to show 16 by 16 pixel image at X=100 & Y=100 but am stuck. The image is to be displayed over the top of whatever is on the page. Altering the top and left properties don't do anything.
Whatever I do, the image seems to show up at pos 0,0.
shimDiv.style.position = 'absolute';
shimDiv.style.top = 100;
shimDiv.style.left = 100;
shimDiv.style.width = "1000px"; // todo: change me!
shimDiv.style.height = "1000px"; // todo: change me!
shimDiv.style.zIndex = 3;
document.body.appendChild(shimDiv);
$("#shim").append('<img id="icon" src="../Images/info.png" alt="todo:" />');
That's because you've not specified the units for the top and left positions:
shimDiv.style.top = '100px';
shimDiv.style.left = '100px';
Hmm, maybe this is an oversimplification, but this should accomplish what you are asking:
$('body').append('<img id="icon" src="../Images/info.png" style="position: absolute; top: 100px; left: 100px; width: 1000px; height: 1000px; z-index: 1000;" />');
It seems in your code sample that the creation code for the parent div element is missing, but the above code should be all that is necessary.
Hope that helps...
Related
I have a multilayer canvas
My html:
<div style='display:inline-block; width:100%; position:relative; top: 0px; left: 0px;'>
<div id='canvas_map_1' class='canvas canvas_map' style='overflow:scroll; background-color: #B4B4B4; z-index: 1; position:absolute; left:133px; top:0px;'></div>
<div id='canvas_map_2' class='canvas canvas_map' style='overflow:scroll; z-index: 2; position:absolute; left:133px; top:0px;'></div>
...
</div>
Given a set of coordinates I want to know if there are some images under it. I'm using Raphael and I want to try the getElementByPoint function (as alternative I could snap the coordinates to a grid and iterate all the images of the canvas to see if some have the same application point (top left corner) and it works but I thinks that will be slow, so I'm looking for faster solutions).
My javascript:
var paper_map_1 = Raphael(canvas_map_1, '100%', '100%');
var image = paper_map_1.image(image_selected, 27, 42, 33, 27);
var myimage = paper_map_1.getElementByPoint(30, 50) // or (27, 42) but shouldn't do difference
console.log(myimage) // null
myimage is null while I expected something because there is an image in that position on that canvas. I can select it, move it, etc. Off course the code is much simplified but I don't know what to add.
jsfiddle
I have a div with some element inside it and I would like to allow the scrolling of the div until the last element.
This is what happens when I scroll:
And this is how I would like to make it:
Is it possible to do it?
Well, it is quite simple without any javascript:
HTML:
<div>
<section>hello</section>
<section>hello</section>
<section>hello</section>
<section>hello</section>
<section>hello</section>
<section>hello</section>
<section>hello</section>
</div>
CSS:
section { height: 100px; }
section:last-child { height: 100%; }
div {
overflow: scroll;
height: 400px;
border: 1px solid black;
}
See fiddle. The concept is just to use the parent div height as a height for the last item.
Try achieve this using JS. Set a bottom margin to a last category equal to wrapper height minus last category height.
var wrapperHeight = $("#wrapper").innerHeight();
var lastCategory = $(".category:last-child");
var lastCategoryHeight = lastCategory.height();
var bottomMargin = wrapperHeight - lastCategoryHeight;
lastCategory.css({margin: "0 0 "+bottomMargin+"px 0"});
DEMO
Also it can be done with scrollIntoView, by scrolling into view the last element, this is the JS snippet:
items = document.querySelectorAll("section");i = items[items.length-1];i.scrollIntoView();
And this is the jsfiddle code
I am attempting to create a function where by a button is clicked and the background image is selected for a div. Here is what I have started below but it does not seem to work can anyone point out where Im going wrong... :)
<style type="text/css">
#txt{
width: auto;
height: auto;
border: solid #000;
}
</style>
<script type="text/javascript">
function backg1(){
var test = new string ();
test = document.getElementById('txt');
test.style.backgroundImage="url('cloud.jpg')";
}
</script>
</head>
<body>
<div id="txt">
<input type="button" value="BG1" onclick="backg1()"/>
</div>
Since your <div> originally contains nothing but the button input, it has no size outside the boundaries of that button. You will need to set an explicit width and height (along with position: relative) to see the background.
I would recommend setting them to the same dimensions as your image.
/* in the CSS */
#txt{
/* use the width, height of your image */
width: 400px;
height: 250px;
position: relative;
border: solid #000;
}
Or if you need to set them dynamically:
function backg1() {
test = document.getElementById('txt');
test.style.backgroundImage="url('cloud.jpg')";
// <div> needs a width & height for the background image to be visible outside the
// bounds of the one element contained in the div.
test.style.width = "400px";
test.style.height = "250px";
test.style.position = "relative";
}
There is a javascript error at this line.
var test = new string ();
You can make it like var test = new String(); Then it should work.
Moreover, i observed that you are creating string object and then you are overriding with DOM object. Not sure why it so like that. You can just create a variable like var test;
I want to be able to move an object from one position to another using buttons for example 3 buttons left right and center that always put the object at the exact same positions everytime.
I have tried using style.position="absolute" but it moves left or right or whatever position depending on it last position never the same three positions.
Which one of static, absolute, fixed, relative, inherit would be best to use in this case? and is it possible to get an example of how a particular object would be set to a particular position thanks in advance
position: absolute tells the browser HOW to position your element, but not WHERE to position your element. You still need to position your element by setting left or right and top or bottom values in your css.
Given some markup:
<button id="left">LEFT</button>
<button id="right">RIGHT</button>
<button id="center">CENTER</button>
<div id="wrapper">
<div id="thingy"/>
</div>
and some styles:
#wrapper {
position: relative;
margin-top: 10px;
}
#thingy {
margin: 0 auto;
width: 25px;
height: 25px;
background-color: #f69;
}
You can move the thingy this way:
var thingy = document.getElementById('thingy');
document.getElementById('left').onclick = function() {
thingy.style.position = 'absolute';
thingy.style.right = null;
thingy.style.left = 0;
};
document.getElementById('right').onclick = function() {
thingy.style.position = 'absolute';
thingy.style.left = null;
thingy.style.right = 0;
};
document.getElementById('center').onclick = function() {
thingy.style.position = 'inherit';
thingy.style.left = null;
thingy.style.right = null;
};
Code is posted at: http://jsfiddle.net/TXWfh/1/
You could make it work with absolute, but I think relative might work best in your case.
HTML
<div id="test"></div>
<input type="button" id="left" value="Left">
<input type="button" id="middle" value="Middle">
<input type="button" id="right" value="Right">
CSS
#test {
position: relative;
width: 100px;
height: 100px;
background-color: red;
}
input {
padding: 4px;
}
JavaScript
var test = document.getElementById("test");
document.getElementById("left").onclick = function() {
test.style.left = "0px";
};
document.getElementById("middle").onclick = function() {
test.style.left = "250px";
};
document.getElementById("right").onclick = function() {
test.style.left = "500px";
};
Live example
Don't know what you are trying to do, but it really doesn't matter if you don't have a lot of surrounding html code that can get affected by your choice of position value.
following 3 fiddles do same things but with subtle difference that they have different value for position attribute.
http://jsfiddle.net/9uQT8/3/
http://jsfiddle.net/9uQT8/4/
http://jsfiddle.net/9uQT8/5/
try clicking left center right. and see. I used jQuery though, super cool JS framework.
I have an element on the page that I've already centered horizontally and vertically (It's a jQuery UI Modal Dialog), and want to resize it using .animate() like this:
<div id="element" style="width: 100px; height: 100px;">
Hi Stack Overflow!
</div>
<script type="text/javascript">
$('#element').animate({ height: "200px" });
</script>
That works fine, except the element only grows downwards. What I'm trying to do is have the element grow vertically in both directions (in this case 50px in each direction) so it stays centered. Is there a way that it can be done?
Live Demo
var growEl = $("#grow"),
curHeight = $("#grow").height(),
curTop = growEl.offset().top,
newHeight = 200,
newMargin = curTop -(newHeight -curHeight)/2;
if(newMargin < 0){
newMargin = 0;
}
$("#grow").animate({height:newHeight+"px", marginTop:newMargin + 'px'});
Formula for figuring out what to make the margin
NewTopMargin = CurrentMargin-(NewHeight-OldHeight)/2
Thanks #bobsoap for reminding me to use offset.top
margin-top: -50px, height: 50px;
That sort of thing (sorry its not code formatted) i'm sure you can insert it correctly.