Jquery .Position() Strangeness While Using CSS3 Rotate Attribute

jQuery .position() strangeness while using CSS3 rotate attribute

// Needed to read the "real" position
$.fn.adjustedPosition = function() {
var p = $(this).position();
return {
left: p.left - this.data('dx'),
top: p.top - this.data('dy')
}
};

$(function() { 

var img = $('img'),
pos;

// Calculate the delta
img.each(function() {
var po = $(this).position(), // original position
pr = $(this).addClass('rot').position(); // rotated position

$(this).data({
dx: pr.left - po.left, // delta X
dy: pr.top - po.top // delta Y
});
});

// Read the position
pos = img.adjustedPosition();
alert(pos.left + '/' + pos.top);

// Write the position
img.css(pos);

// Read the position again
pos = img.adjustedPosition();
alert(pos.left + '/' + pos.top);

});

Live demo: http://jsfiddle.net/2gVL4/4/

So what is going on here:

  1. The CSS code that rotates the image is stored inside a special CSS class. I do this because I want to read the original position of the image (before rotating). Once I read that original position, I apply the .rot class, and then read the position again to calculate the difference (delta), which is stored inside the element's data().

  2. Now, I can read the position via the custom method adjustedPosition (which is defined above). This method will read the position of the element and then subtract the delta values stored inside the data() of the element.

  3. To write the position, just use the css(pos) method like normally.

Div position slightly offset when using jquery draggable with HTML local storage

Have you looked at this bug? http://bugs.jquery.com/ticket/8362

I see that your JS is doing everything right but jQuery is returning the wrong value from .css().

Here's a working example: http://db.tt/gCHuZ7zz

And the relevant code changes:

        note.draggable({ stop: function () {

var left = this.offsetLeft;
var top = this.offsetTop;

localStorage.setItem("left", left);
localStorage.setItem("top", top);

}
});

function updatePosition(note) {

var left = localStorage.getItem("left");
var top = localStorage.getItem("top");
note.css({ left: left + "px", top: top + "px" });

note[0].offsetTop = top;
note[0].offsetLeft = left

}

How to make a vertical line in HTML

Put a <div> around the markup where you want the line to appear to next, and use CSS to style it:

.verticalLine {  border-left: thick solid #ff0000;}
<div class="verticalLine">  some other content</div>

Asynchronous WPF Commands

I've been able to refine the original sample down and have some advice for anyone else running into similar situations.

First, consider if BackgroundWorker will meet the needs. I still use AsyncCommand often to get the automatic disable function, but if many things could be done with BackgroundWorker.

But by wrapping BackgroundWorker, AsyncCommand provides command like functionality with asynchronous behavior (I also have a blog entry on this topic)

public abstract class AsyncCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public event EventHandler RunWorkerStarting;
public event RunWorkerCompletedEventHandler RunWorkerCompleted;

public abstract string Text { get; }
private bool _isExecuting;
public bool IsExecuting
{
get { return _isExecuting; }
private set
{
_isExecuting = value;
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}

protected abstract void OnExecute(object parameter);

public void Execute(object parameter)
{
try
{
onRunWorkerStarting();

var worker = new BackgroundWorker();
worker.DoWork += ((sender, e) => OnExecute(e.Argument));
worker.RunWorkerCompleted += ((sender, e) => onRunWorkerCompleted(e));
worker.RunWorkerAsync(parameter);
}
catch (Exception ex)
{
onRunWorkerCompleted(new RunWorkerCompletedEventArgs(null, ex, true));
}
}

private void onRunWorkerStarting()
{
IsExecuting = true;
if (RunWorkerStarting != null)
RunWorkerStarting(this, EventArgs.Empty);
}

private void onRunWorkerCompleted(RunWorkerCompletedEventArgs e)
{
IsExecuting = false;
if (RunWorkerCompleted != null)
RunWorkerCompleted(this, e);
}

public virtual bool CanExecute(object parameter)
{
return !IsExecuting;
}
}

Background image jumps when address bar hides iOS/Android/Mobile Chrome

This issue is caused by the URL bars shrinking/sliding out of the way and changing the size of the #bg1 and #bg2 divs since they are 100% height and "fixed". Since the background image is set to "cover" it will adjust the image size/position as the containing area is larger.

Based on the responsive nature of the site, the background must scale. I entertain two possible solutions:

1) Set the #bg1, #bg2 height to 100vh. In theory, this an elegant solution. However, iOS has a vh bug (http://thatemil.com/blog/2013/06/13/viewport-relative-unit-strangeness-in-ios-6/). I attempted using a max-height to prevent the issue, but it remained.

2) The viewport size, when determined by Javascript, is not affected by the URL bar. Therefore, Javascript can be used to set a static height on the #bg1 and #bg2 based on the viewport size. This is not the best solution as it isn't pure CSS and there is a slight image jump on page load. However, it is the only viable solution I see considering iOS's "vh" bugs (which do not appear to be fixed in iOS 7).

var bg = $("#bg1, #bg2");

function resizeBackground() {
bg.height($(window).height());
}

$(window).resize(resizeBackground);
resizeBackground();

On a side note, I've seen so many issues with these resizing URL bars in iOS and Android. I understand the purpose, but they really need to think through the strange functionality and havoc they bring to websites. The latest change, is you can no longer "hide" the URL bar on page load on iOS or Chrome using scroll tricks.

EDIT: While the above script works perfectly for keeping the background from resizing, it causes a noticeable gap when users scroll down. This is because it is keeping the background sized to 100% of the screen height minus the URL bar. If we add 60px to the height, as swiss suggests, this problem goes away. It does mean we don't get to see the bottom 60px of the background image when the URL bar is present, but it prevents users from ever seeing a gap.

function resizeBackground() {
bg.height( $(window).height() + 60);
}

CSS triangle containing text

For your plan B (to center the text within the triangle both vertically and horizontally), which I prefer as solution, you could add this css rule:

.up p {
text-align: center;
top: 80px;
left: -47px;
position: relative;
width: 93px;
height: 93px;
margin: 0px;
}

Try it here:

.up {  width: 0px;  height: 0px;  border-style: inset;  border-width: 0 100px 173.2px 100px;  border-color: transparent transparent #007bff transparent;  float: left;  transform: rotate(360deg);  -ms-transform: rotate(360deg);  -moz-transform: rotate(360deg);  -webkit-transform: rotate(360deg);  -o-transform: rotate(360deg);}
.up p { text-align: center; top: 80px; left: -47px; position: relative; width: 93px; height: 93px; margin: 0px;}
<div class="up">  <p>some information text goes here    <p></div>


Related Topics



Leave a reply



Submit