How to move CSS background based on cursor/finger position without using background-position property? - css

I'm working on a small app where the user move the background with his mouse or his finger on the screen.
You can find a demo here: http://kaleidoscope-experiment.herokuapp.com/
The background-position is defined by the position of the cursor/finger using this kind of code:
// mobile
position = e.touches[0].pageX + 'px ' + e.touches[0].pageY + 'px';
// desktop
position = e.pageX + 'px ' + e.pageY + 'px';
However, I have significant lags on mobile and tablet and the background is "clipping".
Do you know if there is any alternative to background-position (transform?), to move the background based on the cursor/mouse position?

If you want to roll your own just use a combination of the jQuery mousemove function in combination with CSS3 transforms. Something along the lines of this:
$( "#target" ).mousemove(function( event ) {
var msg = "Handler for .mousemove() called at ";
msg += event.pageX + ", " + event.pageY;
console.log( "<div>" + msg + "</div>" );
$("#movingobject").css('transform', 'translate(' + event.pageY /4 + 'px,' + -event.pageX /4+ 'px)');
});
Here is the reference fiddle that i got from stackoverflow
http://jsfiddle.net/ZmZhw/
There are number of js plugin that are available which will give u some smooth transition and performance also.

Related

processe indicator change layout SAPUI5

hello someone can help me with this ?
I what to change my process indicator from SAPUI5 to make something like this
enter image description here
the idea is show how much percent are favor about something and how much percent is against
thanks
you can create a custom control like this or added some code to the onAfterRendering event.
ProgressIndicator.extend("ProgressIndicatorEx", {
renderer: {},
onAfterRendering: function() {
if (ProgressIndicator.prototype.onAfterRendering) {
ProgressIndicator.prototype.onAfterRendering.apply(this, arguments);
}
var percentValue = this.getPercentValue();
this.$().find("#" + this.getId() + "-bar").html('<span id="' + this.getId() + '-textLeft" class="sapMPIText sapMPITextRight" style="color: #FFF; margin-right:8px">' + percentValue + '%</span>');
var rightBar = this.$().find("#" + this.getId() + "-remainingBar");
rightBar.html('<span id="' + this.getId() + '-textRight" class="sapMPIText sapMPITextRight" style="color: #FFF">' + (100 - percentValue) + '%</span>');
rightBar.css("background-color", "#c00")
rightBar.css("border-color", "#c00")
}
demo: https://jsbin.com/yawiqam/edit?js,output

How to move popup div along with marker in leaflet

I have a map drawn using leaflet. In that map I have drawn several markers at different locations. On clicking the marker I am creating a popup div that shows some message.
I have the code:
markers.on("click", function(d) {
div.html("This is Some info").style(
"left", (d3.event.pageX) + "px").style(
"top", (d3.event.pageY - 80) + "px");
}
So the div appears in the location where I clicked i.e. at the location of the marker.
Now if I drag the map the markers move along with the map but the div remains at the same location.
How can I change the style of div with drag event so that the div also moves along with marker?
I was trying the following code in the drag event method:
if(d.code==clickedcode)
{
console.log((map.latLngToLayerPoint([ d.lat, d.lon ]).x) + "px , "
+(map.latLngToLayerPoint([ d.lat, d.lon ]).y - 80) + "px");
div.style["left"]=(map.latLngToLayerPoint([ d.lat, d.lon ]).x) + "px";
div.style["top"]=(map.latLngToLayerPoint([ d.lat, d.lon ]).y - 80) + "px";
}
But it still shows the same pixel position as previous time.
You could try using the map 'drag' event to calculate the shift and then apply that to your div location. Something along these lines...
var startCoords;
var startLat;
var startLng;
var movedCoords;
var shiftLat;
var shiftLng;
map.on('dragstart', function() {
startCoords = map.getCenter();
startLat = startCoords.lat;
startLng = startCoords.lng;
console.log("STARTED FROM: " + startLat + "," +startLng);
});
map.on('drag', function() {
movedCoords = map.getCenter();
shiftLat = startLat - movedCoords.lat;
shiftLng = startLng - movedCoords.lng;
console.log("Shifted: " + shiftLat + "," + shiftLng);
//apply shift to custom div here...
});

Auto fit tag clouds in a div

I have some tag clouds placed within a div. For each tag cloud there is a thumbs down icon and I used sliding tag css from http://www.cssflow.com/snippets/sliding-tags
If a tag cloud is greater than 80px, it does not fit inside that div. I used word wrap and also fittext plugin from jquery but I could not manage to make it work. Could you suggest what could be the possible solution for it?
I have created fiddle for it: http://jsfiddle.net/aexqoe8n/1/
In Js side :
resultDiv = document.getElementById('tokenCloudResultDiv');
resultDiv.innerHTML = '';
resultDiv .innerHTML="<ul class='tags blue' style='list-style-type: none;padding:5px;'>";
for (i in json) {
addToken(resultDiv, i, json[i]);
}
resultDiv.innerHTML=resultDiv.innerHTML+"</ul>";
function addToken(target, key, result) {
var str;
var weight;
if(result['tagWeight']>80)
$("#tokenCloudResultDiv").fitText(1.2);
weight= "style='font-size: " + result['tagWeight'] + "px;'";
if(addedAutomatically)
str = "<li><div><a " + weight + " >" + result['tagName'] +"</a><span class='thumbsDownIcon' id ='" + tagId + "' style='display:block;cursor:pointer;' ></span></div></li>";
target.innerHTML += str;
}

rotate image on horizontal scroll

I'm using this code for letting a image rotate on scroll, and it's working great:
<script>
$(window).scroll(function() {
var theta = $(window).scrollTop() % Math.PI;
$('#leftgear').css({ transform: 'rotate(' + theta + 'rad)' });
$('#rightgear').css({ transform: 'rotate(-' + theta + 'rad)' });
});
</script>
Instead of .scrollTop, I use .scrollLeft because my site is horizontally scrolling. I got this code from: http://demosthenes.info/blog/718/Rotating-Elements-With-Page-Scroll-In-JQuery
The example on this site has 2 images. They are turning in opposite directions and with different speed. How can I achieve this? I looked in de source of the demo site and noticed -webkit-transform. I guess this has to do with it? Can someone help?
I want my image to turn the other side and I want it to turn at slower speed.
Very simply, just apply a different transform to each element. In the example you give, you have the #rightgear element rotate -theta rads while the #leftgear element rotates theta rads. You can apply a small modifier to one of these to change the speed, like:
<script>
$(window).scroll(function() {
var theta = $(window).scrollTop() % Math.PI;
$('#leftgear').css({ transform: 'rotate(' + theta + 'rad)' });
$('#rightgear').css({ transform: 'rotate(-' + theta + 'rad)' });
$('#slowgear').css({ transform: 'rotate(' + 0.25*theta + 'rad)' });
$('#fastgear').css({ transform: 'rotate(' + 10*theta + 'rad)' });
});
</script>

How to avoid jsPlumb connectors break off while dragging the dynamically created divs

I am new to jsPlumb. I have created dynamic divs by reading a table from database and I have connected these divs by using JsPlumb connectors. The divs have been made draggable. The connectors are showing up correctly when the page loads, but when I drag or move the divs the connectors breaks off and moved to top. If I create a static divs which are not coming from database then the connectors are working perfectly and when I drag/move the divs then the connectors are also moving along the divs connected to source and target divs.
After doing a complete search on google and by going through jsPlumb documents, I did not find any solution for this.
Can anyone tell me as what should be done to have the connectors stay connected to source and destination even when we drag or move the divs.
Here is what I have done:
JsPlumb Code for one connection:
jsPlumb.ready(function() {
jsPlumb.connect({
source: "FDiv1",
target: "IDiv1",
connector: ["Flowchart"],
anchors: ["RightMiddle", "LeftMiddle"],
paintStyle: {
lineWidth: 2,
strokeStyle: "rgb(189, 11, 50)",
outlineColor: "#666",
outlineWidth: 1
},
hoverPaintStyle: { strokeStyle: "#7ec3d9" },
detachable: false,
endpoint: "Blank",
endpointsOnTop: false,
deleteEndpointsOnDetach: false,
endpointStyle: { fillStyle: "#a7b04b" },
overlays: [ "Arrow" ]
});
jsPlumb.draggable($(".bg"));
});
I have hardcoded the source and target here which later I should be getting these dynamically to connect all other divs.
Here is the dynamic divs code in code behind.
foreach (DataRow dt in ds.Tables[0].Rows)
{
HtmlGenericControl myDiv = new HtmlGenericControl("div");
HtmlGenericControl myDiv2 = new HtmlGenericControl("div");
//********Source Div**************
myDiv.ID = "FDiv" + j;
myDiv.Attributes.Add("class", "bg");
myDiv.Style.Add(HtmlTextWriterStyle.MarginLeft, "30px");
myDiv.Style.Add(HtmlTextWriterStyle.MarginTop, top + "px");
myDiv.InnerHtml = "Filename: " + dt["Column1"].ToString() + "<br>" + myDiv.ID;
PlaceHolder1.Controls.Add(myDiv);
//************Target Div****************
myDiv2.ID = "IDiv" + j;
myDiv2.Attributes.Add("class", "bg");
myDiv2.Style.Add(HtmlTextWriterStyle.MarginLeft, "260px");
myDiv2.Style.Add(HtmlTextWriterStyle.MarginTop, top + "px");
myDiv2.InnerHtml = "Filename: " + dt["Column2"].ToString() + "<br>" + myDiv2.ID;
PlaceHolder1.Controls.Add(myDiv2);
top = top + 140;
j = j + 1;
}
Instead of making the DIV draggable as jsPlumb.draggable($(".bg"));
Try making the DIV draggable during its creation.
i.e.
Source Div
myDiv.ID = "FDiv" + j;
myDiv.Attributes.Add("class", "bg");
myDiv.Style.Add(HtmlTextWriterStyle.MarginLeft, "30px");
myDiv.Style.Add(HtmlTextWriterStyle.MarginTop, top + "px");
myDiv.InnerHtml = "Filename: " + dt["Column1"].ToString() + "<br>" + myDiv.ID;
jsPlumb.draggable($('#FDiv' + j)); // make it draggable
PlaceHolder1.Controls.Add(myDiv);
`
||y for target DIV
I had a similar issue with dynamically created divs and solved it by not using jsPlumb.draggable(element) but instead jQuery and jsPumb.repaint(element) as follows (the example is a bit overkill with the use of repaint but you get the picture):
element.draggable({
step: function () {
jsPlumb.repaint(element);
},
drag:function(){
jsPlumb.repaint(element);
},
stop:function(){
jsPlumb.repaint(element);
}
});
Hope this helps you.

Resources