Hi I'm using tooltips on my site to give links and certain images hints/descriptions. I wanted for my tooltips to be able to have different styles. I pass this variable into the JS file and the code does work to an extent..
The error I am receiving is: The first rollover I make sets the style for all other rollovers, even those that are sending a variable for a different style..
Mouseover HTML
(text, size, style) - this is an example of two different onmouseovers sending a diff style
onmouseover="tooltip.show('About Us', 89,'cont1');"
onmouseover="tooltip.show('Archive', 89,'cont2');"
JS
var tooltip=function(){
var id = 'tt';
var top = 3;
var left = 3;
var maxw = 300;
var speed = 10;
var timer = 40;
var endalpha = 95;
var alpha = 0;
var tt,t,c,b,h, no;
var ie = document.all ? true : false;
return{
show:function(v,w,no){
if(tt == null){
tt = document.createElement('div');
tt.setAttribute('id',id + no);
t = document.createElement('div');
t.setAttribute('id',id + 'top');
c = document.createElement('div');
c.setAttribute('id',id + 'text');
b = document.createElement('div');
b.setAttribute('id',id + 'bot');
tt.appendChild(t);
tt.appendChild(c);
tt.appendChild(b);
document.body.appendChild(tt);
tt.style.opacity = 0;
tt.style.filter = 'alpha(opacity=0)';
document.onmousemove = this.pos;
}
tt.style.display = 'block';
c.innerHTML = v;
tt.style.width = w ? w + 'px' : 'auto';
if(!w && ie){
t.style.display = 'none';
b.style.display = 'none';
tt.style.width = tt.offsetWidth;
t.style.display = 'block';
b.style.display = 'block';
}
if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
h = parseInt(tt.offsetHeight) + top;
clearInterval(tt.timer);
tt.timer = setInterval(function(){tooltip.fade(1)},timer);
},
pos:function(e){
var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
tt.style.top = (u - h) + 'px';
tt.style.left = (l + left) + 'px';
},
fade:function(d){
var a = alpha;
if((a != endalpha && d == 1) || (a != 0 && d == -1)){
var i = speed;
if(endalpha - a < speed && d == 1){
i = endalpha - a;
}else if(alpha < speed && d == -1){
i = a;
}
alpha = a + (i * d);
tt.style.opacity = alpha * .01;
tt.style.filter = 'alpha(opacity=' + alpha + ')';
}else{
clearInterval(tt.timer);
if(d == -1){tt.style.display = 'none'}
}
},
hide:function(){
clearInterval(tt.timer);
tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
}
};
}();
If I follow your code, it looks like your handing in style and using it as part of the name of the ID for the div your creating for the tool tip (I'm assuming your styling things based on ID name). If this is all true, it looks like your issue is that after the first tooltip is created (when tt == null at the top of your show function) you never change the ID of the tool tip after that. The ID only updates on create, so the style will not change once its created.
of course I could be completely wrong about this (I am after all out of coffee at this moment...)
Sorry for not actually answering your question, but you shouldn't invent the wheel. There're SO many libraries out there, that does the exact same thing. Here is 50 to get you started.
Related
How to insert a auto-incrementing id from 1 before info.name in jQuery? I use below code to get aggregated data, for example, each quessionId has many related reasons. Below is my partial code of .js code in jQuery:
function fmtQuestionsByID(id,callback){
if(!DATA.questions[id] || !$('#card_'+id) )return;
var project = DATA.projects[DATA.questions[id].projectId];
if(!project)return;
var issueQuestionLists = DATA.alltags.reduce(function(a,b){
if(a[b['quessionId']]) {
a[b['quessionId']].push({name:b['name'],color:b['color'],description:b['description'],reason:b['reason'],question:b['question'],issueId:b['issueId'],department:b['department'],_id:b['quessionId']})
} else{
a[b['quessionId']] = [{name:b['name'],color:b['color'],description:b['description'],reason:b['reason'],question:b['question'],issueId:b['issueId'],department:b['department'],_id:b['quessionId']}]
}
return a;
},{});
for(var i=0;i < DATA.questions[id].tags.length;i++){
var lid = DATA.questions[id].tags[i];
for(var l in issueQuestionLists){
var lb = issueQuestionLists[l]
for(var c=0;c< lb.length;c++){
var lc = lb[c];
if(lc._id == lid){
var info = lc;
console.log('info', info);
$('.tags_question').append('['+info.name+']' + info.description + '。' + 'Reason: '+info.reason+ '。' ||'[no data]' );
}
}
}
}
}
And I use below html to get above data
<div id="questioninfo">
<span class="tags_question"></span>
</div>
In the console, I do have those dictionaries, but why the page only get one dict?
Thanks so much for any help.
Thanks so much for Swati's help, to change .html to .append.
You can define a variable which will store the value of count and then whenever you need to print it first increment it and then add that as well with the data which you are already appending .So your code will look like below :
var count = 0; //declare this
for (var i = 0; i < DATA.questions[id].tags.length; i++) {
//..other codes
var lc = lb[c];
if (lc._id == lid) {
count++; //increment
var info = lc;
console.log('info', info);
//add with other datas
$('.tags_question').append(count + '[' + info.name + ']' + info.description + '。' + 'Reason: ' + info.reason + '。' || '[no data]');
}
}
I am a beginner and used jscript in my php page to hide/show table using the following code:
<script type="text/javascript">
function setTable(what){
if(document.getElementById(what).style.display=="none"){
document.getElementById(what).style.display="block";
}
else if(document.getElementById(what).style.display=="block"){
document.getElementById(what).style.display="none";
}
}
</script>
and display/hide table2 using:
<img src="/icons/index.png" width="55" height="55">
My problem is that while using this code, in case of any browser activity, the table is reset back to default state. I am struggling to enable users once they show/hide table it should continue even when refresh/or navigate happens within the pages. I know I should use cookies but have no idea. Please someone guide me.
thanks
Use cookies to store table state.
<script type="text/javascript">
var tableState = getCookie("table2state");
if (tableState != "") {
document.getElementById('table2').style.display = tableState;
}
function setTable(what) {
if (document.getElementById(what).style.display == "none") {
document.getElementById(what).style.display = "block";
setCookie(what + 'state', "block", 365);
} else if (document.getElementById(what).style.display == "block") {
document.getElementById(what).style.display = "none";
setCookie(what + 'state', "none", 365);
}
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
return "";
}
</script>
I recently found using stage.toDataURL for saving stage content as a image.(KineticJS version 5.0.1)
Custom Shape drawFunc content with core HTML5 canvas object will not applied in image after saving an image.
###################### Images
Please see attached image to get better clarity.
1)Original canvas(stage).
https://f.cloud.github.com/assets/1320926/2300781/c8167c30-a107-11e3-9b61-acf94005c252.png
2)Image after saving stage content
test1
https://f.cloud.github.com/assets/1320926/2300784/fc5343b6-a107-11e3-8c69-07cccc4d7ac4.png
We see that the vertical string "Sample Text" will not saved or it's invisible whatever it is.
###################### Code
1)Here is my code of shape object where line was printed vertically "Sample Text"
var sampleText = new Kinetic.Shape({
drawFunc: function(context) {
var ctx=this.getContext()._context;
ctx.beginPath();
var maxWidth = 2;
var lineHeight = 25;
var x = 415;
var y = 700;
var text = 'Sample Text';
ctx.font = '15pt Calibri';
ctx.fillStyle = 'green';
//Function for vetical text display
wrapText(ctx, text, x, y, maxWidth, lineHeight);
ctx.closePath();
ctx.fill();
ctx.restore();
},
});
//wrapText function if needed
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split('');
var line = '';
//alert(context);
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = 20;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.fillText(line, x, y);
}
2)Stage code for saving an image
stage.toDataURL({
callback: function(dataUrl) {
/*
* here you can do anything you like with the data url.
* In this tutorial we'll just open the url with the browser
* so that you can see the result as an image
*/
$('#theimage').attr('src',dataUrl);
$.ajax({
type: "POST",
url: "save.php",
data: { imageData: dataUrl },
});
}
});
Please advise.
Thanks in advance for your time and consideration.
-Naitik.
You chould not use this.getContext()._context because _context is "private" property (starts with undescore). It is not KinetiJS way. Use context as in an example: http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-shape-tutorial/
Your solution:
var sampleText = new Kinetic.Shape({
drawFunc: function(ctx) {
ctx.beginPath();
var maxWidth = 2;
var lineHeight = 25;
var x = 0;
var y = 0;
var text = 'Sample Text';
ctx.setAttr("font", '15pt Calibri');
ctx.setAttr('fillStyle','green');
//Function for vetical text display
wrapText(ctx, text, x, y, maxWidth, lineHeight);
ctx.closePath();
// KineticJS specific context method
ctx.fillStrokeShape(this);
}
});
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split('');
var line = '';
//alert(context);
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context._context.measureText(testLine);
var testWidth = 20;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.fillText(line, x, y);
}
Demo: http://jsbin.com/xolen/1/edit
Note: KineticJS's context has not measureText method, so you have to use _context property. I think measureText method will be added soon.
http://www.juriseodesign.com/clock/Sydney.php
My clock count javascript shows errors. I have my background changes working, but after adding the count clock javascript, the clock doesn't work. I guess it is caused by "collisions" with Mootools and regular jQuery, but I don't have any idea how to fix it. Could anybody can help this for me? Also, how to connect to other paged when I click the one of the city in the dropdown menu?
Thank you so much!
// JavaScript Document
//initial time
var h_current = -1;
var m1_current = -1;
var m2_current = -1;
var s1_current = -1;
var s2_current= -1;
function flip (upperId, lowerId, changeNumber, pathUpper, pathLower){
var upperBackId = upperId+"Back";
$(upperId).src = $(upperBackId).src;
$(upperId).setStyle("height", "64px");
Uncaught TypeError: Object # has no method 'setStyle' (repeated 315 times)
$(upperId).setStyle("visibility", "visible");
$(upperBackId).src = pathUpper+parseInt(changeNumber)+".png";
$(lowerId).src = pathLower+parseInt(changeNumber)+".png";
$(lowerId).setStyle("height", "0px");
$(lowerId).setStyle("visibility", "visible");
var flipUpper = new Fx.Tween(upperId, {duration: 200, transition: Fx.Transitions.Sine.easeInOut});
flipUpper.addEvents({
'complete': function(){
var flipLower = new Fx.Tween(lowerId, {duration: 200, transition: Fx.Transitions.Sine.easeInOut});
flipLower.addEvents({
'complete': function(){
lowerBackId = lowerId+"Back";
$(lowerBackId).src = $(lowerId).src;
$(lowerId).setStyle("visibility", "hidden");
$(upperId).setStyle("visibility", "hidden");
} });
flipLower.start('height', 64);
}
});
flipUpper.start('height', 0);
}//flip
function retroClock(){
// get new time
now = new Date();
h = now.getHours();
m1 = now.getMinutes() / 10;
m2 = now.getMinutes() % 10;
s1 = now.getSeconds() / 10;
s2 = now.getSeconds() % 10;
if(h < 12)
ap = "AM";
else{
if( h == 12 )
ap = "PM";
else{
ap = "PM";
h -= 12; }
}
//change pads
if( h != h_current){
flip('hoursUp', 'hoursDown', h, 'Single/Up/'+ap+'/', 'Single/Down/'+ap+'/');
h_current = h;
}
if( m2 != m2_current){
flip('minutesUpRight', 'minutesDownRight', m2, 'Double/Up/Right/', 'Double/Down/Right/');
m2_current = m2;
flip('minutesUpLeft', 'minutesDownLeft', m1, 'Double/Up/Left/', 'Double/Down/Left/');
m1_current = m1;
}
if (s2 != s2_current){
flip('secondsUpRight', 'secondsDownRight', s2, 'Double/Up/Right/', 'Double/Down/Right/');
s2_current = s2;
flip('secondsUpLeft', 'secondsDownLeft', s1, 'Double/Up/Left/', 'Double/Down/Left/');
s1_current = s1;
}
}
setInterval('retroClock()', 1000);
I just looked at jQuery reference, setStyle method does not exist.
You should use css method instead :
$(upperId).css('height','64px;');
Looking at this code, there are many things, that don't match with jQuery methods.
For example: if you want change src attribute to upperId element, in jQuery you should use attr method:
// $(upperId).src = $(upperBackId).src; // WRONG
$(upperId).attr('src', $(upperBackId).attr('src')); // CORRECT
If you want reference an element with its id you must prepend # before the element id:
var upperBackId = "#" + upperId + "Back";
You try to use Mootools methods with jQuery. There is no setStyle() method in jQuery.
Like others have suggested you should use $(..).css( ...) function since setStyle does not exist in jQuery.
I usually also prefer to use $(...).hide() and $(...).show() instead of $(...).css("visibility","hidden") or $(...).css("visibility","visible") since "visibility" still takes space and is the same as setting opacity to 0 - If I hide the element, I don't want it to use any space...
I have a website that consists of index.php and otherpage.php. Both of these pages use
include_once("header.inc")
header.inc implements a jscript file like this
<script type="text/javascript" src="script.js"></script>
the jscript file lets me use a nice looking dropdown menu.
the problem is that the menu only works properly on index.php, and not otherpage.php
What is really getting me is that on otherpage.php it's not that the menu doesnt work AT ALL, it just doesn't work partly. The menu will highlight but not dropdown.
You can see for yourself
index.php
otherpage.php
Is there something about sharing a jscript file between PHP pages that I should know?
Here are the relevant jscript contents for the menu:
var menu = function() {
var t = 15, z = 50, s = 6, a;
function dd(n) {
this.n = n;
this.h = [];
this.c = []
}
dd.prototype.init = function(p, c) {
a = c;
var w = document.getElementById(p), s = w.getElementsByTagName('ul'), l = s.length, i = 0;
for(i; i < l; i++) {
var h = s[i].parentNode;
this.h[i] = h;
this.c[i] = s[i];
h.onmouseover = new Function(this.n + '.st(' + i + ',true)');
h.onmouseout = new Function(this.n + '.st(' + i + ')');
}
}
dd.prototype.st = function(x, f) {
var c = this.c[x], h = this.h[x], p = h.getElementsByTagName('a')[0];
clearInterval(c.t);
c.style.overflow = 'hidden';
if(f) {
p.className += ' ' + a;
if(!c.mh) {
c.style.display = 'block';
c.style.height = '';
c.mh = c.offsetHeight;
c.style.height = 0
}
if(c.mh == c.offsetHeight) {
c.style.overflow = 'visible'
} else {
c.style.zIndex = z;
z++;
c.t = setInterval(function() {
sl(c, 1)
}, t)
}
} else {
p.className = p.className.replace(a, '');
c.t = setInterval(function() {
sl(c, -1)
}, t)
}
}
function sl(c, f) {
var h = c.offsetHeight;
if((h <= 0 && f != 1) || (h >= c.mh && f == 1)) {
if(f == 1) {
c.style.filter = '';
c.style.opacity = 1;
c.style.overflow = 'visible'
}
clearInterval(c.t);
return
}
var d = (f == 1) ? Math.ceil((c.mh - h) / s) : Math.ceil(h / s), o = h / c.mh;
c.style.opacity = o;
c.style.filter = 'alpha(opacity=' + (o * 100) + ')';
c.style.height = h + (d * f) + 'px'
}
return {
dd : dd
}
}();
Thanks for your time
On the index.php page you are forgetting
<script type="text/javascript">
var menu = new menu.dd("menu");
menu.init("menu", "menuhover");
</script>
Put it at the bottom of otherpage.php or put it in a footer.php to be included at the bottom of the page.
seem that the second page (not the index.php) does not have the
<script type="text/javascript">
var menu = new menu.dd("menu");
menu.init("menu", "menuhover");
</script>
so the menu isn't created.
index.php includes this code at the end of the page that initializes your menu:
<script type="text/javascript">
var menu = new menu.dd("menu");
menu.init("menu", "menuhover");
</script>
otherpage.php does not include that code so the code is never initialized and hooked up to your HTML.
Incidentally, you can debug this kind of issue yourself by putting a breakpoint in the .init() method in your code. In index.php you see that the breakpoint is hit and if you look at the calling stack, you can see where it is called from. If you put the same breakpoint in otherpage.php, you can see that it is not hit, thus never called.