Drag an image within a div with left/right constraints - jquery-ui

I'm trying to nest an image in a div and make it draggable on the X axis within this div.
Here's a jsFiddle to illustrate my point
I'd like the blue box to stop dragging once the user reaches its left/right edges, he shouldn't be able to see any of the red wrapper anymore.
Imagine the blue box is a very wide image, I want to be able to drag it from its left side to its right side without exceeding its width. It means you can't drag it to the right when you reached its left edge, and you can't drag it to the left when you reached its right side.
In a nutshell, when you reach the edges of the image, you can't drag it any more.
How can I set that kind of behavior ?
I tried playing with containment but I couldn't achieve what I wanted.
Thanks for your help.
HTML :
<div id="wrapper">
<p id="timeline"><img src="" alt="Timeline" width="2000" height="400"/></p>
</div>
CSS :
#wrapper {
width: 800px;
height: 400px;
background-color : red;
overflow: hidden;
cursor: w-resize;
}
#timeline {
width: 2000px;
height: 400px;
margin: 0 auto;
background-color: blue;
}
JS :
$("#timeline").draggable({
axis: "x"
});

You were correct to use the containment option but you'll want to pass in array of coordinates to constrain the draggable.
Per the jQuery UI docs the containment option can take an Array in format [x1, y1, x2, y2]. In your case the y values are irrelevant because you are already constraining the draggable to the x axis, so you can simply pass in 0.
For the x values 0 will work for x1 since the image is already starting on the right edge. For x2 you'll need to use:
(width of parent - width of image)
So
(800 - 2000) = -1200
Therefore you can pass the following in for the containment option:
containment: [-1200, 0, 0, 0]
Live Example - http://jsfiddle.net/LMXLj/

Related

How to get horizontal padding in Photoswipe?

I'm using photoswipe with disabled zoom, like suggested here, but also want to avoid very landscapish images to reach beneath the prev/next arrows. Is there an easy way to achieve this?
Just found a solution, by
1) giving "padding" to the .pswp__item, like so:
.pswp__item {
left: 50px;
right: 50px;
}
2) updating the horizontal viewport size before resize:
pswp.listen('beforeResize', function(){
pswp.viewportSize.x = pswp.viewportSize.x - 100;
);
I can't comment on #johjoh's answer, so I'm going to copy it here and add the 3rd step required to get this working if you are using PhotoSwipe's getThumbBoundsFn.
1) giving "padding" to the .pswp__item, like so:
.pswp__item {
left: 50px;
right: 50px;
}
2) updating the horizontal viewport size before resize:
pswp.listen('beforeResize', function(){
pswp.viewportSize.x = pswp.viewportSize.x - 100;
);
3) Ensuring the getThumbBoundsFn properly calculates the x value by subtracting one side (50px in this example) from your calculated value:
bounds = {
x: {x} - 50,
y: ...,
w: ...,
}

Left positioned item on iOS gets larger on iframe

I've implemented a simple left-pull burger menu in a mobile webpage that lives inside an iframe. However, it's behaving strangely on iPhones. We are using Bootstrap for the general page layout and stuff.
Using WeInRe I've noticed the following behaviour: in an iframe with 320px in fixed width, if I add, say, left: 50px to the body of the page inside it, this body moves 50px to the left just fine, but also starts to display 370px in width, instead of 320px as before.
The problem is worse: as the correct left value is a percentage, the body gets that bigger width, and after that the left is recalculated, making the menu larger than the viewport.
What the hell is happening here? Is this some sort of known bug of Mobile Safari?
Unfortunately, there's no public available code for this issue yet...
This is the relevant code:
.offcanvas {
left: 0;
position: relative;
}
.offcanvas.active {
left: 75%;
overflow: hidden;
}
.sidebar {
position: fixed;
background-color: #5c008a;
top: 0;
left: -75%;
width: 75%;
height: 100%;
}
.offcanvas.active .sidebar {
left: 0;
}
$('[data-toggle="offcanvas"]').click(function() {
$('.offcanvas').toggleClass('active');
});
<body class="offcanvas">
[...]
<div class="sidebar">[...]</div>
[...]
</body>
Here's a sample, based on a series of side menus from a tutorial (click the left or right push options).

How can i show a jquery menu which opens from right to left side?

I want to show a menu on clicking on a link. The menu should appear from right to left with show function.
I have already tried
$('.amenu').show("slide", { direction: "right" }, "fast");
But it comes sliding. I want the same effect as
$('.amenu').show("fast");
but menu originating from top right corner.
I've made this fiddle for you which you can use in any way. The idea is that the menu is put in position with this css:
position: absolute;
top: 0;
right: 0;
If you play around you can make it stick anywhere.
The animation is also done with CSS using this class and you can leave only width if you want a slide animation:
.animate {
height: 100px;
width: 300px;
transition: width 1s, height 1s;
-moz-transition: width 1s, height 1s;
-webkit-transition: width 1s, height 1s;
-o-transition: width 1s, height 1s;
}
And a simple jQuery to toggle the class.
https://jsfiddle.net/mtucpL3v/
Further reading: CSS transitions

How do I create a pie chart from VML shapes?

I realize that VML is deprecated as of IE9, but I need compatibility with older versions. I have been looking at highcharts, and how they make their otherwise SVG-based charts work with IE 6.0+, and the answer is VML.
As I have been able to render some examples in IE8 with the help of Highcharts, I have also been able to extract some source code examples to play with. This specific chart is the one I am trying to understand and recreate. I do not care about animations, tool tip texts or highlighting. I basically want to understand how i can draw polygons that include curves, and in this case, as a part of a half donut-shaped pie chart.
The code below represents an example that has three slices of a pie chart, inside a frame-shape. I have been playing around with the numbers, but I cannot figure out exactly what the following values represent:
path = "
wr1120,933,4670,4483,0,2600,1170,2248
at2058,1870,3733,3545,2081,2510,500,2510 x e"
They obviously represent some kind of coordinates, angles or distances, but after both playing around with them, and searching for documentation, I couldn't come to an understanding.
But, this I found out (values named wr1-8 and at1-8):
-wr seems to represent the outer circle, while at represents the inner cut-out (hole in the donut)
values wr1-2 and at1-2 seems to represent some kind of mask of the total circle.
values wr3-4 and at3-4 seems to represent some kind of arch rotation, or something else arch related. all numbers 1-4 are equal in all slices
values 5-6 and 7-8 on both at and wr seems to represent continual coordinates, where the next slice starts where the previous stopped. However, I cannot find a pattern when I change the numbers. I cannot find the system.
So what does these values actually represent? Or better yet, where can i find documentation that explains this?
Below is the complete code of a three sliced half donut pie chart example:
<!doctype html public '-//W3C//DTD html 4.0 Transitional//EN'><%# Language=VBScript%>
<html xmlns:v='urn:schemas-microsoft-com:vml'>
<STYLE>v\:* {behavior:url(#default#VML);}}</STYLE>
<shape style="POSITION: absolute; MARGIN-TOP: 0px; WIDTH: 1px;
DISPLAY: inline-block; HEIGHT: 1px; VISIBILITY: visible;
MARGIN-LEFT: 0px; TOP: 0px; BEHAVIOR: url(#default#VML);
LEFT: 0px" xmlns="urn:schemas-microsoft-com:vml" stroke-linejoin="round"
coordsize = "10,10" filled = "t" fillcolor = "#00FF00" stroked = "t"
strokecolor = "white" strokeweight = ".75pt"
path = " wr1120,933,4670,4483,0,2600,1170,2248
at2058,1870,3733,3545,2081,2510,500,2510 x e"><fill style="DISPLAY:
inline-block; BEHAVIOR: url(#default#VML)"
xmlns="urn:schemas-microsoft-com:vml" type = "solid" opacity = "1">
</fill></shape>
<shape style="POSITION: absolute; MARGIN-TOP: 0px; WIDTH: 1px;
DISPLAY: inline-block; HEIGHT: 1px; VISIBILITY: visible;
MARGIN-LEFT: 0px; TOP: 0px; BEHAVIOR: url(#default#VML);
LEFT: 0px" xmlns="urn:schemas-microsoft-com:vml" stroke-linejoin="round"
coordsize = "10,10" filled = "t" fillcolor = "#dddf00" stroked = "t"
strokecolor = "white" strokeweight = ".75pt" path = "
wr1120,933,4670,4483,1170,2248,2509,975
at2058,1870,3733,3545,2713,1890,2081,2510 x e">
<fill style="DISPLAY: inline-block; BEHAVIOR: url(#default#VML)"
xmlns="urn:schemas-microsoft-com:vml" type = "solid" opacity = "0.5">
</fill></shape>
<shape style="POSITION: absolute; MARGIN-TOP: 0px; WIDTH: 1px;
DISPLAY: inline-block; HEIGHT: 1px; VISIBILITY: visible;
MARGIN-LEFT: 0px; TOP: 0px; BEHAVIOR: url(#default#VML);
LEFT: 0px" xmlns="urn:schemas-microsoft-com:vml" stroke-linejoin="round"
coordsize = "10,10" filled = "t" fillcolor = "#ed561b" stroked = "t"
strokecolor = "white" strokeweight = ".75pt"
path = " wr1120,933,4670,4483,2509,975,2579,961
at2058,1870,3733,3545,2746,1883,2713,1890 x e">
</shape>
wr means "clockwise arch"
at means "arch to"
The 8 numbers represent 4 (x, y)-paired coordinates in both cases. The two first pairs represent the upper left, and lower right coordinates of the bounding box of an elipse. To put it simple, it is the coordinates on the canvas where you press the mouse, and where you release the mouse while drawing a circle shape in MS Paint.
The two last pairs represent coordinates, which gives vectors from the center of the circle, to the given coordinate. The angles produced will be the starting and ending angle of the given slice.
the angles only decides the cutting point inbetween the bounding circle, and the vector. In other words, the angles only gives a point, not a side. Both the inner and the outer circle needs matching vector angles, to give a nice presentation, where the lines that seperates the sides of the donut slices, would all cross in the center of the donut hole.
For more info about all possible commands for the path, see:
http://www.w3.org/TR/1998/NOTE-VML-19980513#_Toc416858391
I would suggest creating in SVG first, then converting to VML, if that's possible for you. I recently figured out how to do so.
Essentially it involves using this stylesheet: http://www.cs.sjsu.edu/faculty/pollett/masters/Semesters/Fall03/JulieNabong/translator.xsl
You also need Notepad++ and its XML Tools plugin.
1 - Open up the SVG file in Notepad++
2 - Go to Plugins --> XML Tools --> XSL Transformations
3 - Select the translator.xsl stylesheet
4 - Click on "Transform"
Here's the thesis' demo page:
http://www.cs.sjsu.edu/faculty/pollett/masters/Semesters/Fall03/JulieNabong/index.shtml?display.html

Hide the word "followers" from Twitter follow button

Is there a way to modify the Twitter 'follow' button to display the number of followers in a bubble but hide the word "followers"? I basically want my 'follow' button to look the same as the 'tweet' button.
The current code looks like this:
<a href="https://twitter.com/User" class="twitter-follow-button" data-show-count="true"
data-show-screen-name="false" data-dnt="true">Follow #User</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id))
{js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
I had this same issue and solved it by basically hiding the word 'followers' and creating a fake right edge to the bubble, if that makes sense. So, you have to wrap the button in its own div, then hide the overflow of that div and set the width to the exact point where the word disappears and the height precisely to the height of the button you are using. Here's a code example:
#titter-div {
border-radius: 4px; /* to mimic the curved edges of the count box */
border-right: 1px solid #AAAAAA; /* this is the width and color of the count box border */
height: 20px; /* this height works for the medium button */
width: 88px; /* precise width to hide the word */
overflow: hidden; /* actually hides the word */
}
This worked for me to create exactly what you are looking for. Hope this helps.
To display the number of followers in a bubble but hide the word "followers", here is the code I use with an iframe :
<iframe
src="//platform.twitter.com/widgets/follow_button.html?screen_name=Vacance_Luberon&show_screen_name=false"
style="width: 88px; height: 20px;"
allowtransparency="true"
frameborder="0"
scrolling="no">
</iframe>

Resources