Follow these steps to set up some tooltips that display Ajax content:
<!DOCTYPE html> <html> <head> <title>Pittsburgh, Pennsylvania</title> <link rel="stylesheet" href="styles/styles.css"/> </head> <body> <h2>Pittsburgh, Pennsylvania</h2> <p>Pittsburgh is the second-largest city in the US Commonwealth of Pennsylvania and the county seat of Allegheny County. Regionally, it anchors the largest urban area of Appalachia and the Ohio River Valley, and nationally, it is the 22nd-largest urban area in the United States. The population of the city in 2010 was 305,704 while that of the seven-county metropolitan area stood at 2,356,285. <a href="http://infoboxes/downtown.html">Downtown Pittsburgh</a> retains substantial economic influence, ranking at 25th in the nation for jobs within the urban core and 6th in job density.</p> <p>The characteristic shape of Pittsburgh's central business district is a triangular tract carved by the confluence of the Allegheny and Monongahela rivers, which form the Ohio River. The city features 151 high-rise buildings, 446 bridges, two inclined railways, and a pre-revolutionary fortification. Pittsburgh is known colloquially as "The City of Bridges" and "The Steel City" for its <a href="http://infoboxes/bridges.html">many bridges</a> and former steel manufacturing base.</p> <p>The warmest month of the year in Pittsburgh is July, with a 24-hour average of 72.6°F. Conditions are often humid, and combined with the 90°F (occurring on an average of 8.4 days per annum), a considerable <a href="http://infoboxes/heatindex.html">heat index</a> arises.</p> <script src="scripts/jquery.js"></script> <script src="scripts/scripts.js"></script> </body> </html>
<a href ="http://infoboxes/downtown.html" class="infobox">Downtown Pittsburgh</a>
<!DOCTYPE html> <html> <head> <title>Downtown Pittsburgh</title> </head> <body> <img src="../images/downtown.jpg"/> <p>Downtown Pittsburgh</p> </body> </html>
As you can see, the file is extremely small and simple.
infoboxes
directory alongside the main page. Save your simple HTML file to this directory, and then create more simple files one for each link in the main document. ui-tooltip-purple
class to the styles.css
file:/*! Purple tooltip style */ .ui-tooltip-purple .ui-tooltip-titlebar, .ui-tooltip-purple .ui-tooltip-content{ border-color: #c1c3e6; color: #545aba; } .ui-tooltip-purple .ui-tooltip-content{ background-color: #f1f2fa; } .ui-tooltip-purple .ui-tooltip-titlebar{ background-color: #d9daf0; } .ui-tooltip-purple .ui-state-default .ui-tooltip-icon{ background-position: -102px 0; } .ui-tooltip-purple .ui-tooltip-icon{ border-color: #c1c3e6; } .ui-tooltip-purple .ui-tooltip-titlebar .ui-state-hover{ border-color: #c1c3e6; }
scripts.js
file:<script src="scripts/jquery.js"></script>
<script src="scripts/jquery.qtip.min.js"></script>
<script src="scripts/scripts/js"></script>
</body>
</html>
scripts.js
and we'll get started with our document ready function:$(document).ready(function(){ });
qtip()
method in a slightly different way than we have before. Inside the qtip()
method, we need to easily get to the information about just the link we're working with, so we're going to use jQuery's each()
method to loop through them one at a time. That will look like this:$(document).ready(function(){ $('a.infobox').each(function(){ $(this).qtip() }); });
title
attributes, and that's what the qTip plugin is looking for by default. However, we can override that default to insert any content we'd like into our tooltips.$(document).ready(function(){
$('a.infobox').each(function(){
$(this).qtip({
content: {
text: 'Loading...'
}
});
})
});
Now when you refresh the page in the browser, you'll see the tooltips contain the Loading... text.
$(document).ready(function(){
$('a.infobox').each(function(){
$(this).qtip({
content: {
text: 'Loading...'
},
show: {
event: 'click',
solo: true
},
});
})
});
$(document).ready(function(){
$('a.infobox').bind('click', function(e){e.preventDefault()});
$('a.infobox').each(function(){
Now if you refresh the page in the browser, you'll see that the tooltips appear when we click on the links — clicking the links no longer takes us off to a new page.
bind()
method directly to the end of the each()
method we wrote earlier. The new JavaScript will look like this:$(document).ready(function(){
$('a.infobox').each(function(){
$(this).qtip({
content: {
text: 'Loading...'
},
show: {
event: 'click',
solo: true
},
});
}).bind('click', function(e){e.preventDefault()});
});
$(document).ready(function(){
$('a.infobox').each(function(){
$(this).qtip({
content: {
text: 'Loading...',
},
show: {
event: 'click',
solo: true
},
style: {
classes: 'ui-tooltip-purple ui-tooltip-shadow'
}
});
}).bind('click', function(e){e.preventDefault();});
});
Now when you refresh the page in the browser, you'll see that we have purple tooltips that have a drop shadow. We're getting closer.
To tell the tooltips to fetch content via Ajax, all we have to do is pass the URL of the content we'd like to fetch. In this case, we've already linked out to that content. We just have to grab the link URL from each link. That's easily accessible to us by using the attr()
method of jQuery. That will look like this:
$(this).attr('href')
In this case, $(this)
is referring to the current link. I call the attr()
method and pass that method the attribute I would like to fetch, in this case the href
attribute of the link contains the information that I want. The attr()
method can be used to fetch any attribute — an src
attribute of an image, a title
attribute of any element, a cellspacing
attribute of a table, and so on:
$('img').attr('src') $('p').attr('title') $('table').attr('cellspacing')
href
attribute of our link, we'll use that to tell the tooltip which URL to use to get the content for our tooltip:$(document).ready(function(){
$('a.infobox').each(function(){
$(this).qtip({
content: {
text: 'Loading...',
ajax: {
url: $(this).attr('href')
}
},
show: {
event: 'click',
solo: true
},
style: {
classes: 'ui-tooltip-purple ui-tooltip-shadow'
}
});
}).bind('click', function(e){e.preventDefault()});
});
Now, let's make a couple of other final tweaks to the tooltips to make them even better.
index.html
file and add a title
attribute that contains the text to display at the top of the tooltips:<a href ="http://infoboxes/downtown.html" class="infobox" title="Downtown Pittsburgh">Downtown Pittsburgh</a> ... <a href ="http://infoboxes/bridges.html" class="infobox" title="Pittsburgh Bridges">many bridges</a> <a href ="http://infoboxes/heatindex.html" class="infobox" title="Beating the Heat">heat index</a>
title
attribute of these links in much the same way that we fetched the URL of the href
attribute and pass it to qTip as the title text for the tooltip. While we're at it, we can also pass in a true
value for button to show a small close button at the top-right of the tooltip:$(document).ready(function(){
$('a.infobox').each(function(){
$(this).qtip({
content: {
text: 'Loading...',
ajax: {
url: $(this).attr('href')
},
title: {
text: $(this).attr('title'), button: true
}
},
show: {
event: 'click',
solo: true
},
style: {
classes: 'ui-tooltip-purple ui-tooltip-shadow'
}
});
}).bind('click', function(e){e.preventDefault()});
});
Now when you refresh the browser, you'll see a darker title bar with a close button appear at the top of each tooltip.
unfocus
so that the tooltip will be hidden when the link loses focus or when the site visitor clicks the close button on the tooltip:$(document).ready(function(){
$('a.infobox').each(function(){
$(this).qtip({
content: {
text: 'Loading...',
ajax: {
url: $(this).attr('href')
},
title: {
text: $(this).attr('title'),
button: true
}
},
show: {
event: 'click',
solo: true
},
hide: 'unfocus',
style: {
classes: 'ui-tooltip-purple ui-tooltip-shadow'
}
});
}).bind('click', function(e){e.preventDefault()});
});
$(document).ready(function(){
$('a.infobox').each(function(){
$(this).qtip({
content: {
text: 'Loading...',
ajax: {
url: $(this).attr('href')
},
title: {
text: $(this).attr('title'),
button: true
}
},
position: {
my: 'top center',
at: 'bottom center'
},
show: {
event: 'click',
solo: true
},
hide: 'unfocus',
style: {
classes: 'ui-tooltip-purple ui-tooltip-shadow'
}
});
}).bind('click', function(e){e.preventDefault()});
});
Now, if you refresh the page in the browser and click the links, you'll see the tooltips slide into place from their default position.
effect
value to false
. That way the tooltip will show up where it's supposed to without the animation of sliding into place. The other problem is that, depending on the size of your browser window, sometimes the tooltips are cut off and display outside the screen area. To make sure this doesn't happen, we'll set the viewport
value to the window like the following:$(document).ready(function(){
$('a.infobox').each(function(){
$(this).qtip({
content: {
text: 'Loading...',
ajax: {
url: $(this).attr('href')
},
title: {
text: $(this).attr('title'),
button: true
}
},
position: {
my: 'top center',
at: 'bottom center',
effect: false,
viewport: $(window)
},
show: {
event: 'click',
solo: true
},
hide: 'unfocus',
style: {
classes: 'ui-tooltip-purple ui-tooltip-shadow'
}
});
}).bind('click', function(e){e.preventDefault()});
});
18.188.40.207