I am using the “lightbox” effect to open my images which uses javascript. I am also using jQuery for the changing banner at the top of the page. Ever since I put the jQuery script in, the “lightbox” script stopped working. When I remove the jQuery script, the “lightbox” script works again. Obviously they are conflicting for some reason. I have included the relevant code below. Can anyone see why this is happening? Thanks.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- TemplateBeginEditable name="Head" -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Company</title>
<link href="../include/global.css" rel="stylesheet" type="text/css" />
<!-- TemplateEndEditable -->
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="js/lightbox.js"></script>
<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
</head>
<body>
<center>
<!-- Top Start -->
<div class="top">
</div>
<!-- Top End -->
<div class="wrapper2">
<!-- jQuery Start -->
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">
function slideSwitch() {
var $active = $('#slideshow IMG.active');
if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
// use this to pull the images in the order they appear in the markup
var $next = $active.next().length ? $active.next()
: $('#slideshow IMG:first');
// uncomment the 3 lines below to pull the images in random order
// var $sibs = $active.siblings();
// var rndNum = Math.floor(Math.random() * $sibs.length );
// var $next = $( $sibs[ rndNum ] );
$active.addClass('last-active');
$next.css({opacity: 0.0})
.addClass('active')
.animate({opacity: 1.0}, 1000, function() {
$active.removeClass('active last-active');
});
}
$(function() {
setInterval( "slideSwitch()", 5000 );
});
</script>
Thanks for the reply. I tried adding the below snippet of code, but the jQuery slideshow didn’t work. What do you mean I can use jQuery instead of $ for a guaranteed solution? Sorry, Javascript isn’t my strong point.
That’s because even though the returned function knows about the jQuery value of $, by the time the slideSwitch function is run the prototype one takes over.
Use this instead, where you pass $ to the slideSwitch function:
Thanks again for the reply (I think we’re getting close). Below is what I now have, and it is working, but the jQuery slideshow stops on the 2nd slide (there are a total of 7 slides that it runs through). Any idea on why it stops after the 2nd slide?
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript">
function slideSwitch($) {
var $active = $('#slideshow IMG.active');
if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
// use this to pull the images in the order they appear in the markup
var $next = $active.next().length ? $active.next()
: $('#slideshow IMG:first');
// uncomment the 3 lines below to pull the images in random order
// var $sibs = $active.siblings();
// var rndNum = Math.floor(Math.random() * $sibs.length );
// var $next = $( $sibs[ rndNum ] );
$active.addClass('last-active');
$next.css({opacity: 0.0})
.addClass('active')
.animate({opacity: 1.0}, 1000, function() {
$active.removeClass('active last-active');
});
}
jQuery.noConflict();
jQuery(function($) {
setTimeout(function () {
slideSwitch($);
}, 5000 );
});
</script>