Here is my code:
var tagDes = document.createElement('DIV');
tagDes.className = 'tagDes';
tagDes.style.cssText = 'position:absolute;'+
'background:#282828;'+
'color:#fff;'+
'padding:10px;'+
'top:'+(posX+hei)+'px;'+
'left:'+(posY+wid)+'px;'+
'font-size:10pt;';
tagDes.onmouseout = function(){
$(this).remove();
};
$('#main-container').append(tagDes);
$('.tagDes').append(array[5]+'<a class="tagMenu">sdsdssds</a>');
posX, posY, hei, wid is reffering to an element for positioning. array[5] is a string.
I want to hover for a li and create div that contain link(tagMenu class) inside(looks like title attribute). But when I hover the link inside that div, the div will remove(). What I looking for is when I hover the link the div still visible and it will removed from page when I mouseout from it. Any suggestion? Please help me.
try something like this:
tagDes.onmouseout = function(e){
if (e.toElement.parentNode == this || e.toElement == this) {
return;
}
$(this).remove();
};
If I understood you correctly you want to bind onmouseout event to inner link element. Then the code can be like this (I made use of more jQuery to simplify it):
var $tagDes = $('<div class="tagDes"></div>').css({
background: '#282828',
color: '#fff',
padding: '10px',
top: (posX + hei) + 'px',
left: (posY + wid) + 'px',
fontSize: '10pt'
})
.append(array[5] + '<a class="tagMenu">sdsdssds</a>')
.appendTo('#main-container');
$tagDes.on('mouseleave', function() {
$tagDes.remove();
});
Related
I want to change the color from white to black and/or black to white of my navbar-toggle.
But the problem is when it reach a sepecific div with a specific class like 'white' or 'black' the color changes when the scroll begins.
var stickyOffset = $(".navbar-toggle").offset();
var $contentDivs = $("section");
$(document).scroll(function() {
$contentDivs.each(function(k) {
var _thisOffset = $(this).offset();
var _actPosition = _thisOffset.top - $(window).scrollTop();
if (_actPosition < (stickyOffset.top + $('.navbar-toggle').height()/2) && _actPosition + $(this).height() - $('.navbar-toggle').height()/2 > 0) {
$(".bar1, .bar2, .bar3, .navbar-span").removeClass("white black").addClass($(this).hasClass("white") ? "white" : "black");
}
});
});
Now my jsfiddle but it changes very fast and I don't know what i'm doing wrong.
http://jsfiddle.net/xarlyblack/8mn4bucw/
Thank you in advance!
Best,
Carl
I dont know if I understand correctly your problem, but it seems to me that you have a logic error with your color label assignment, I think it should be like this:
...
if (_actPosition < (stickyOffset.top + $('.navbar-toggle').height()/2) &&
_actPosition + $(this).height() - $('.navbar-toggle').height()/2 > 0) {
$(".bar1, .bar2, .bar3, .navbar-span").removeClass("white black")
.addClass($(this).hasClass("white") ? "black" : "white");
}
...
And here is an updated jsfiddle in which I think it is properly working
As others have already pointed out, in your jsfiddle the two classes should be switched, but if i understand you correctly, on initial page load the classes also do not match if you, for example already scrolled down and make a page reload/refresh or you come from a anchor link.
To fix this i would suggest you also run the class-switch after document load like this:
var stickyOffset = $(".navbar-toggle").offset();
var $contentDivs = $("section");
$(document).scroll(function() {
checkcolor();
});
$(document).ready(function() {
checkcolor();
});
function checkcolor()
{
$contentDivs.each(function(k) {
var _thisOffset = $(this).offset();
var _actPosition = _thisOffset.top - $(window).scrollTop();
if (_actPosition < (30 + $('.navbar-toggle').height()/2) && _actPosition + $(this).height() - $('.navbar-toggle').height()/2 > 0) {
$(".bar1, .bar2, .bar3, .navbar-span").removeClass("white black").addClass($(this).hasClass("white") ? "black" : "white");
}
});
}
I added a function call on document ready, and removed your stickyOffset Variable, because on page reload/refresh you are positioned in the middle of the site, the offset is way of. Your stickyOffset needs to be a fixed value. I just added the default number of 30 in.
See a fiddle here:
http://jsfiddle.net/5gcemfz0/3/
Seems like the problem is in
$(".bar1, .bar2, .bar3, .navbar-span").removeClass("white black").addClass($(this).hasClass("white") ? "white" : "black");
that place. It says that 'if $(this) has class 'white' add 'white' else add 'black'. Should change places. Hope it helps!
I would like to know how to position a fragment of html to appear next to where the cursor is, specifically on click. So far I have a div which will position itself next to the cursor on click.
shape.on('click', function(event){
if (event.which == 1) {
$("#message").css({
top: event.pageY + 5,
left: event.pageX + 5
}).show();
}
});
But rather what I am after is to not 'show' any existing div, but insert new code 'on the fly' to where the cursor is.
Thank you
Use .html() to insert the text into the div that appears next to your cursor.
Example: $("#message").html("html text that you want");
If you want to continue to add text to message then use .append(), otherwise .html() will overwrite the contents of #message each time.
You just have to append (append) or insert (html) the code you want on that div:
$('#message').html('Hello world!');
Full code:
shape.on('click', function(event){
if (event.which == 1) {
$("#message").css({
top: event.pageY + 5,
left: event.pageX + 5
}).show();
$('#message').html('Hello world!');
}
});
Living demo: http://jsfiddle.net/zEsF5/3/
You can add new elements easily if you do:
shape.on('click', function(event){
if (event.which == 1) {
$("<div>The <strong><em>HTML</em> code</strong> you want<br/>can go here<div>").css({
top: event.pageY + 5,
left: event.pageX + 5,
position: 'absolute'
}).appendTo(shape).show();
}
});
There is a working JS Fiddle
I am trying to incrementally reposition an element by setting the -webkit-transform property to +=200px
But it doesn't work. I'm missing something, but don't know what.
http://jsfiddle.net/yVSDC/
Clicking the div is supposed to move it further down by 200px everytime.
CSS doesn't work like that, and jQuery doesn't pull that data out so you can do it like that.
You'd need to read the value, add 200 and then reset it.
var getTranslatedY = function (element) {
var transform = element.css("transform") || element.css("-webkit-transform") || element.css("-moz-transform");
if (transform == "none") {
return 0;
}
return parseInt((transform.match(/^matrix\(.*?(-?[\d.]+)\)$/) || [])[1]) || 0;
};
$('#m').on(
'click',
function () {
var element = $(this);
element
.css('-webkit-transform',
"translateY(" + (getTranslatedY(element) + 200) + "px)");
});
jsFiddle.
var step = 200;
$('body').on('click', "#m", function(){
$(this).css('-webkit-transform','translateY('+ step +'px)');
step = step+200;
});
And instead -webkit-transform:translateY(100px); in CSS you can set margin-top: 100px;
http://jsfiddle.net/yVSDC/28/
You have to get the Current webkit-transform value and increment it. Alternative Solution is ustin the top CSS Property
You can try this (it's free): http://ricostacruz.com/jquery.transit/
Sample Code:
$('.box').transition({ x: '200px' });
$('.box').transition({ y: '200px' });
$('.box').transition({ x: '200px', y: '200px' });
Is there a simple way to locate all DOM elements that "cover" (that is, have within its boundaries) a pixel with X/Y coordinate pair?
You can have a look at document.elementFromPoint though I don't know which browsers support it.
Firefox and Chrome do. It is also in the MSDN, but I am not so familiar with this documentation so I don't know in which IE version it is included.
Update:
To find all elements that are somehow at this position, you could make the assumption that also all elements of the parent are at this position. Of course this does not work with absolute positioned elements.
elementFromPoint will only give you the most front element. To really find the others you would have to set the display of the front most element to none and then run the function again. But the user would probably notice this. You'd have to try.
I couldn't stop myself to jump on Felix Kling's answer:
var $info = $('<div>', {
css: {
position: 'fixed',
top: '0px',
left: '0px',
opacity: 0.77,
width: '200px',
height: '200px',
backgroundColor: '#B4DA55',
border: '2px solid black'
}
}).prependTo(document.body);
$(window).bind('mousemove', function(e) {
var ele = document.elementFromPoint(e.pageX, e.pageY);
ele && $info.html('NodeType: ' + ele.nodeType + '<br>nodeName: ' + ele.nodeName + '<br>Content: ' + ele.textContent.slice(0,20));
});
updated: background-color !
This does the job (fiddle):
$(document).click(function(e) {
var hitElements = getHitElements(e);
});
var getHitElements = function(e) {
var x = e.pageX;
var y = e.pageY;
var hitElements = [];
$(':visible').each(function() {
var offset = $(this).offset();
if (offset.left < x && (offset.left + $(this).outerWidth() > x) && (offset.top < y && (offset.top + $(this).outerHeight() > y))) {
hitElements.push($(this));
}
});
return hitElements;
}
When using :visible, you should be aware of this:
Elements with visibility: hidden or opacity: 0 are considered visible,
since they still consume space in the layout. During animations that
hide an element, the element is considered to be visible until the end
of the animation. During animations to show an element, the element is
considered to be visible at the start at the animation.
So, based on your need, you would want to exclude the visibility:hidden and opacity:0 elements.
I am having problem getting my tooltip to work when using the jQuery load() function. The tooltip works fine if i don't use load() to load external dynamic data.
I googled and found the i may need to use live(), but I can't figure how to get it to work. Any suggestion?
thank you!!
Here is my scripts:
function loadEMERContent(uid) {
$("#ActionEWPNBook").load("ajaxLoadDATA.cfm?uid="+uid, null, showLoading);
$("#EWPNBookloading").html('<img src="/masterIncludes/images/ajaxLoading.gif" alt="loading" align="center" />');
}
function showLoading() {
$("#EWPNBookloading").html('');
}
function simple_tooltip(target_items, name){
$(target_items).each(function(i){
$("body").append("<div class='"+name+"' id='"+name+i+"'><p style='padding:5px;'>"+$(this).attr('title')+"</p></div>");
var my_tooltip = $("#"+name+i);
$(this).removeAttr("title").mouseover(function(){
my_tooltip.css({opacity:0.8, display:"none"}).stop().fadeIn(400);
}).mousemove(function(kmouse){
my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
}).mouseout(function(){
my_tooltip.stop().fadeOut(400);
});
});
}
Here is my ajaxLoadDATA.cfm: it returns an unorder list
<li><span title="dynamic title here" class="tipthis">date</span></li>
In your callback function, you need to run the tooltip code against the new items, like this:
function showLoading(data) { //data = the response text, passed from `.load()`
$("#EWPNBookloading").html('');
var items = //get items here, something like $('.giveMeATooltip', data);
var name = //set name, not sure what you're using for this
simple_tooltip(items, name);
}
One side tip, if you change this line:
$("body").append("<div class='"+name+"' id='"+name+i+"'><p style='padding:5px;'>"+$(this).attr('title')+"</p></div>");
To something like this:
$("body").append(
$("<div></div>", { 'class': name, 'id': name + i }).append(
$("<p style='padding:5px;'></p>").text($(this).attr('title'))
)
);
Your tooltip generation will be much faster due to how jQuery can cache document fragments for reuse.
I use a modified version of this tooltip. This version uses .live so loaded content will automatically have tooltip functionality. All you need to do is:
Add this script to your main page (adding it as an external file is perferred)
You need to ensure that the elements that need a tooltip have a class="tipthis" and that the tooltip contents are within the title attribute. Also, the tooltip can contain HTML (e.g. <h1>Tooltip</h1>Hello,<br>World).
You will also need some basic CSS:
#tooltip { background: #f7f5d1; color: #333; padding: 5px; width: 300px; }
Here is the script (using live and it requires jQuery version 1.4+)
/* Tooltip script */
this.tooltip = function(){
// Defaults
var tooltipSpeed = 300; // fadein speed in milliseconds
var xOffset = 20; // Don't use negative values here
var yOffset = 20;
$('.tipthis').live('mouseover',function(e) {
this.t = this.title;
this.title = '';
$('<div></div>', {
id : 'tooltip',
css: {
display : 'none',
position: 'absolute',
zIndex : 1000,
top : (e.pageY - xOffset) + 'px',
left : (e.pageX + yOffset) + 'px'
}
})
.html( this.t )
.appendTo('body');
$('#tooltip').fadeIn(tooltipSpeed);
})
.live('mouseout',function(){
this.title = this.t;
$('#tooltip').remove();
})
.live('mousemove',function(e){
$("#tooltip").css({
top : (e.pageY - xOffset) + 'px',
left: (e.pageX + yOffset) + 'px'
});
})
}
$(document).ready(function(){ tooltip(); });
Here is some sample HTML:
Hover over me!