...

View Full Version : jQuery Quick Question About Variables



exotic
02-05-2011, 03:02 AM
Hi,
im new to js and having a problem with a jQuery plugin i got from nettuts called bettertooltip. im using jQuery-1.5-min.js, if its of any relevance.

How do i set the position as a variable so that later i can change it in the call ?






$.fn.betterTooltip = function(options){

/* Setup the options for the tooltip that can be
accessed from outside the plugin */
var defaults = {
speed: 200,
delay: 200
};

var options = $.extend(defaults, options);

/* Create a function that builds the tooltip
markup. Then, prepend the tooltip to the body */
getTip = function() {
var tTip =
"<div class='tip'>" +
"<div class='tipMid'>" +
"</div>" +
"<div class='tipBtm'></div>" +
"</div>";
return tTip;
}
$("body").prepend(getTip());

/* Give each item with the class associated with
the plugin the ability to call the tooltip */
$(this).each(function(){

var $this = $(this);
var tip = $('.tip');
var tipInner = $('.tip .tipMid');

var tTitle = (this.title);
this.title = "";

var offset = $(this).offset();
var tLeft = offset.left;
var tTop = offset.top;
var tWidth = $this.width();
var tHeight = $this.height();

/* Mouse over and out functions*/
$this.hover(function() {
tipInner.html(tTitle);
setTip(tTop, tLeft);
setTimer();
},
function() {
stopTimer();
tip.hide();
}
);

/* Delay the fade-in animation of the tooltip */
setTimer = function() {
$this.showTipTimer = setInterval("showTip()", defaults.delay);
}

stopTimer = function() {
clearInterval($this.showTipTimer);
}

/* Position the tooltip relative to the class
associated with the tooltip */
setTip = function(top, left){
var topOffset = tip.height();
var xTip = (left-30)+"px";
var yTip = (top-topOffset-60)+"px";
tip.css({'top' : yTip, 'left' : xTip});
}

/* This function stops the timer and creates the
fade-in animation */
showTip = function(){
stopTimer();
tip.animate({"top": "+=20px", "opacity": "toggle"}, defaults.speed);
}
});
};

harbingerOTV
02-05-2011, 08:07 PM
Can you elaborate a little?

This is currently settign the top and left positions as variables:

var tLeft = offset.left;
var tTop = offset.top;

This is applying the variables:

setTip(tTop, tLeft);

exotic
02-06-2011, 02:44 AM
Thanks for the reply Harbinger! :thumbsup:




SUPER NOOBY mistake on my part, sorry about that.
I realized that the whole plugin gets in the way of what im trying to do, so i suppose what I really need to do is just chop it down a bit.



The only thing im trying to accomplish with this plugin is to:

Instead of displaying a single background image (theyre all the same size even though the text/content within them varies in length),
Display a div that contains three images (tipTop.png, tipMid.png, tipBtm.png).

and to

Have tipMid.png repeat itself vertically as many times as it needs to contain the length of the text/content




Im guessing that what i would need to do is change the .tooltip style with js? or rather, change the plugin so that it only changes the .tooltip ?

The ultimate goal is to be able to call the tooltip the way jQuery TOOLS (http://flowplayer.org/tools/demos/tooltip/index.htm) has it set up, [$("#tTip img[title]").tooltip();]
but for instead of a static image, have the tooltip (effectually) change its height to accommodate the content, the way net tuts+ (http://d2o0t5hpnwv4c1.cloudfront.net/234_tooltip/Demo/index.html) has it set up.


Can someone point me in the right direction? All replies are much appreciated.

harbingerOTV
02-07-2011, 07:55 PM
Okay, so you want the jQuery TOOLS version to use 3 divs like the net tuts version, right?

Inside the jquery.tools.js find this line:


f.tools.tooltip={conf:{effect:"toggle",fadeOutSpeed:"fast",predelay:0,delay:30,opacity:1,tip:0,position:["top","center"],offset:[0,0],relative:false,cancelDefault:true,events:{def:"mouseenter,mouseleave",input:"focus,blur",widget:"focus mouseenter,blur mouseleave",tooltip:"mouseenter,mouseleave"},layout:"<div/>",tipClass:"tooltip"},addEffect:function(a,b,c){o[a]=[b,c]}};

You can then make it look like:


f.tools.tooltip={conf:{effect:"toggle",fadeOutSpeed:"fast",predelay:0,delay:30,opacity:1,tip:0,position:["top","center"],offset:[0,0],relative:false,cancelDefault:true,events:{def:"mouseenter,mouseleave",input:"focus,blur",widget:"focus mouseenter,blur mouseleave",tooltip:"mouseenter,mouseleave"},layout:"<div><span class='tooltipTop'/><span class='tooltipBottom'/></div>",tipClass:"tooltip"},addEffect:function(a,b,c){o[a]=[b,c]}};

All that's really changing is this:

layout:"<div/>"

to this:

layout:"<div><span class='tooltipTop'/><span class='tooltipBottom'/></div>"

This will then generate a tooltip like:


<div class="tooltip"><span class="tooltipTop"></span><span class="tooltipBottom"></span>The Toll Tip Text</div>

Then you can just modify the CSS to make it all play nice:


.tooltip {
display:none;
background: #99336c;
font-size:12px;
width:160px;
padding: 26px 20px; /* adjust this to suit */
color:#fff;
}

.tooltip .tooltipTop {
display: block;
background: #e9dd3e;
height: 20px; /* the height of your top image */
position: absolute;
top: 0;
left: 0;
width: 200px; /* the width of the .tooltip + the total horizontal padding. */
}

.tooltip .tooltipBottom {
display: block;
background: #e9dd3e;
height: 20px; /* the height of your bottom image */
position: absolute;
bottom: 0;
left: 0;
width: 200px; /* the width of the .tooltip + the total horizontal padding. */
}

That CSS was taken directly from: http://flowplayer.org/tools/demos/tooltip/index.htm

exotic
02-11-2011, 10:21 PM
thanks again harbingerOTV (http://www.codingforums.com/member.php?u=18597) for bearing with me.


props for the site too, its dope.:cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum