jQuery Get Modal Window working right
I'm trying to get each time you press the arrow, it moves it one more , but currently its only showing the content for:
<div id="dialog" class="window">
One
<a href="#"class="close"/>Close it</a>
</div>
When I click it, it only shows that dialog window dispite having these below it:
<div id="dialog22" class="window">
Two
<a href="#"class="close"/>Close it</a>
</div>
<div id="dialog3" class="window">
Three
<a href="#"class="close"/>Close it</a>
</div>
<div id="dialog4" class="window">
Four
<a href="#"class="close"/>Close it</a>
</div>
FULL CODE:
<!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>
<title>Simple JQuery Modal Window from Queness</title>
<!-- TUTORIAL:::::::: http://sixrevisions.com/tutorials/javascript_tutorial/create-a-slick-and-accessible-slideshow-using-jquery/-->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<style type="text/css">
<!--
/**
* Slideshow style rules.
*/
#slideshow {
margin:0 auto;
width:640px;
height:263px;
//background:transparent url(img/bg_slideshow.jpg) no-repeat 0 0;
background-color:#999;
position:relative;
}
#slideshow #slidesContainer {
margin:0 auto;
width:560px;
height:263px;
overflow:auto; /* allow scrollbar */
position:relative;
}
#slideshow #slidesContainer .slide {
margin:0 auto;
width:540px; /* reduce by 20 pixels of #slidesContainer to avoid horizontal scroll */
height:263px;
}
/**
* Slideshow controls style rules.
*/
.control {
display:block;
width:39px;
height:263px;
text-indent:-10000px;
position:absolute;
cursor: pointer;
}
#leftControl {
top:0;
left:0;
background:transparent url(img/control_left.jpg) no-repeat 0 0;
}
#rightControl {
开发者_C百科 top:0;
right:0;
background:transparent url(img/control_right.jpg) no-repeat 0 0;
}
/**
* Style rules for Demo page
*/
* {
margin:0;
padding:0;
font:normal 11px Verdana, Geneva, sans-serif;
color:#ccc;
}
a {
color: #fff;
font-weight:bold;
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
body {
// background:#393737 url(img/bg_body.jpg) repeat-x top left;
background-color:#fff;
}
#pageContainer {
margin:0 auto;
width:960px;
}
#pageContainer h1 {
display:block;
width:960px;
height:114px;
// background:transparent url(img/bg_pagecontainer_h1.jpg) no-repeat top left;
//background-color:#666;
font-size:4em;
color:red;
text-indent: -10000px;
}
.slide h2, .slide p {
margin:15px;
}
.slide h2 {
font:italic 24px Georgia, "Times New Roman", Times, serif;
color:#ccc;
letter-spacing:-1px;
}
.slide img {
float:right;
margin:0 15px;
}
#footer {
height:100px;
}
#footer p {
margin:30px auto 0 auto;
display:block;
width:560px;
height:40px;
}
-->
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var currentPosition = 0;
var slideWidth = 560;
var slides = $('.slide');
var numberOfSlides = slides.length;
// Remove scrollbar in JS
$('#slidesContainer').css('overflow', 'hidden');
// Wrap all .slides with #slideInner div
slides
.wrapAll('<div id="slideInner"></div>')
// Float left to display horizontally, readjust .slides width
.css({
'float' : 'left',
'width' : slideWidth
});
// Set #slideInner width equal to total width of all slides
$('#slideInner').css('width', slideWidth * numberOfSlides);
// Insert controls in the DOM
$('#slideshow')
.prepend('<span class="control" id="leftControl">Clicking moves left</span>')
.append('<span class="control" id="rightControl">Clicking moves right</span>');
// Hide left arrow control on first load
manageControls(currentPosition);
// Create event listeners for .controls clicks
$('.control')
.bind('click', function(){
// Determine new position
currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
// Hide / show controls
manageControls(currentPosition);
// Move slideInner using margin-left
$('#slideInner').animate({
'marginLeft' : slideWidth*(-currentPosition)
});
});
// manageControls: Hides and Shows controls depending on currentPosition
function manageControls(position){
// Hide left arrow if position is first slide
if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
// Hide right arrow if position is last slide
if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
}
});
</script>
<script>
$(document).ready(function() {
//select all the a tag with name equal to modal
$('#slidesContainer li a[name=modal]').click(function(e) {
//Cancel the link behavior
e.preventDefault();
//Get the A tag
var id = $(this).attr('href');
//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();
//Set heigth and width to mask to fill up the whole screen
$('#mask').css({'width':maskWidth,'height':maskHeight});
//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow",0.8);
//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
//Set the popup window to center
$(id).css('top', winH/2-$(id).height()/2);
$(id).css('left', winW/2-$(id).width()/2);
//transition effect
$(id).fadeIn(2000);
});
//if close button is clicked
$('.window .close').click(function (e) {
//Cancel the link behavior
e.preventDefault();
$('#mask').hide();
$('.window').hide();
});
//if mask is clicked
$('#mask').click(function () {
$(this).hide();
$('.window').hide();
});
});
</script>
<style>
body {
font-family:verdana;
font-size:15px;
}
a {color:#333; text-decoration:none}
a:hover {color:#ccc; text-decoration:none}
#mask {
position:absolute;
left:0;
top:0;
z-index:9000;
background-color:#000;
display:none;
}
#boxes .window {
position:absolute;
left:0;
top:0;
width:440px;
height:200px;
display:none;
z-index:9999;
padding:20px;
}
#boxes .window001 {
position:absolute;
left:0;
top:0;
width:440px;
height:200px;
display:none;
z-index:9999;
padding:20px;
}
#boxes .window002 {
position:absolute;
left:0;
top:0;
width:440px;
height:200px;
display:none;
z-index:9999;
padding:20px;
}
#boxes .window003 {
position:absolute;
left:0;
top:0;
width:440px;
height:200px;
display:none;
z-index:9999;
padding:20px;
}
#boxes .window004 {
position:absolute;
left:0;
top:0;
width:440px;
height:200px;
display:none;
z-index:9999;
padding:20px;
}
#boxes #dialog, #boxes #dialog22, #boxes #dialog3, #boxes #dialog4 {
width:375px;
height:203px;
padding:10px;
background-color:#ffffff;
}
#boxes #dialog1 {
width:375px;
height:203px;
}
#dialog1 .d-header {
background:url(images/login-header.png) no-repeat 0 0 transparent;
width:375px;
height:150px;
}
#dialog1 .d-header input {
position:relative;
top:60px;
left:100px;
border:3px solid #cccccc;
height:22px;
width:200px;
font-size:15px;
padding:5px;
margin-top:4px;
}
#dialog1 .d-blank {
float:left;
background:url(images/login-blank.png) no-repeat 0 0 transparent;
width:267px;
height:53px;
}
#dialog1 .d-login {
float:left;
width:108px;
height:53px;
}
#boxes #dialog2 {
background:url(images/notice.png) no-repeat 0 0 transparent;
width:326px;
height:229px;
padding:50px 0 20px 25px;
}
</style>
</head>
<body>
<ul>
<div id="pageContainer">
<!-- Slideshow HTML -->
<div id="slideshow">
<div id="slidesContainer">
<li><a href="#dialog" name="modal">
<div class="slide">
Content 1
</div>
</a></li>
<li><a href="#dialog22" name="modal">
<div class="slide">
Content 2
</div>
</a></li>
<li><a href="#dialog3" name="modal">
<div class="slide">
Content 3
</div>
</a></li>
<li><a href="#dialog4" name="modal">
<div class="slide">
Content 4
</div>
</a></li>
</div>
</div>
<!-- Slideshow HTML -->
</div>
</ul>
<div style="font-size:10px;color:#ccc">Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License.</div>
<div id="boxes">
<!--contents-->
<div id="dialog" class="window">
One
<a href="#"class="close"/>Close it</a>
</div>
<div id="dialog22" class="window">
Two
<a href="#"class="close"/>Close it</a>
</div>
<div id="dialog3" class="window">
Three
<a href="#"class="close"/>Close it</a>
</div>
<div id="dialog4" class="window">
Four
<a href="#"class="close"/>Close it</a>
</div>
<!-- Start of Login Dialog -->
<div id="dialog1" class="window">
<div class="d-header">
<input type="text" value="username" onclick="this.value=''"/><br/>
<input type="password" value="Password" onclick="this.value=''"/>
</div>
<div class="d-blank"></div>
<div class="d-login"><input type="image" alt="Login" title="Login" src="images/login-button.png"/></div>
</div>
<!-- End of Login Dialog -->
<!-- Start of Sticky Note -->
<div id="dialog2" class="window">
So, with this <b>Simple Jquery Modal Window</b>, it can be in any shapes you want! Simple and Easy to modify : ) <br/><br/>
<input type="button" value="Close it" class="close"/>
</div>
<!-- End of Sticky Note -->
<!-- Mask to cover the whole screen -->
<div id="mask"></div>
</div>
</body>
</html>
Anyone see why this isn't working? Its driving me insane lol.
You need to give each of your new modal windows a higher z-index in the css. Therefore when dialog 2 is created it will sit on top of dialog 1 due to its higher z index.
you could do this in your js file on creation of the window, just make sure your newly created dialog has a higher z index than the last.
精彩评论