javascript - 根据图片大小设置div高度

标签 javascript php jquery html css

所以我在我的 php 代码中填充了三个 javascript 数组,以保存照片的尺寸及其来源。然后,假设尺寸不等于使用 php 的 325x300,我只会在我的页面上显示图像的 325x300 缩略图。

当用户单击其中一个缩略图时,我希望它在 #largeImage div 中打开全尺寸图像,然后取消隐藏它。到目前为止,这是可行的。

但是,我在让潜水调整图像尺寸时遇到了麻烦。我正在使用 php 来获取尺寸(除非在 jQuery 中有更好的方法)。此时 div 没有调整大小,我不确定从这里去哪里。我已经包含了 jquery 和来自 php 代码的输出 html。

<html>
<head>
    <style>
        @import "album_style.css";
    </style>

    <script type="text/javascript" src="jquery-1.10.2.min.js"></script>
    <script>
        $(document).ready(function() {
            var active_index;
            var source_image;
            var index;
            var style;

            $('#largeImage').hide();
            $('#imageBackground').hide();

            $('#gallery img').click(function() {
                index = parseInt( $(this).attr('value'));
                $('#largImage').height(parseInt(picture_y[index]) + 20);
                $('#largImage').width(parseInt(picture_x[index]) + 50);
                $('#largeImage img').attr('src', picture_list[index]);
                $('#largeImage').show() ;
                $('#imageBackground').show() ;
                active_index = index ;
            });

            $('.next').click(function( event ){
                active_index = active_index + 1;
                max_index = array_size - 1;
                if(active_index == array_size)
                {
                    active_index = 0 ;
                }
                source_image = $('#largeImage img').attr('src');
                var newSrc = $('#largeImage img').attr('src')
                    .replace(source_image, picture_list[ active_index]);
                $('#largeImage img').attr('src', newSrc);
            });

            $('.back').click(function(event) {
                active_index = active_index - 1;

                if( active_index < 0 )
                {
                    active_index = array_size - 1;
                }
                source_image = $('#largeImage img').attr('src') ;
                var newSrc = $('#largeImage img').attr('src')
                        .replace(source_image, picture_list[active_index]);
                $('#largeImage img').attr('src', newSrc);
            }) ;

            $('.close').click(function() {
                $('#largeImage').hide();
                $('#imageBackground').hide();
            });
        });
    </script>
</head>

<body>

<div id="container">
    <img src="websitebanner.jpg">
</div>

<div id="nav">
    <ul>
        <li>
            <a href="index.php">Home</a>
        </li>
        <li>
            <a href="news.php">News</a>
        </li>
        <li>
            <a href="events.php">Events</a>
        </li>
        <li>
            <a href="#">The Team</a>
            <ul>
                <li><a href="roster.php">Roster</a></li>
                <li><a href="albums.php">Albums</a></li>
            </ul>
        </li>
        <li>
            <a href="sponsors.php">Sponsors</a>
        </li>
        <li>
            <a href="social.php">Contact Us</a>
        </li>
    </ul>
</div>

<div class='pageHeader'>Test1</div>

<script type='text/javascript'>
    var array_size = 3;
</script>

<script type='text/javascript'>
    var picture_list = new Array(array_size);
    var picture_x = new Array(array_size);
    var picture_y = new Array(array_size);
</script>

<div id='imageBackground'></div>

<div id='largeImage'>
    <input type='button' class='close' value='X'>
    <input type='button' class='back' value='<<'>
    <img src='Test1/Image3.jpg'>
    <input type='button' class='next' value='>>'>
</div>

<div id='gallery'>
    <div id='thumbpanel'>
        <img value='0' src='Thumbnails/Thumb_Image6.jpg'>
        <script type='text/javascript'>
            picture_list[0] = 'Albums/Test1/Image6.jpg';
            picture_x[0] = 386;
            picture_y[0] = 233;
        </script>
        <img value='1' src='Thumbnails/Thumb_Ellie 012.jpg'>
        <script type='text/javascript'>
            picture_list[1] = 'Albums/Test1/Ellie 012.jpg';
            picture_x[1] = 3264;
            picture_y[1] = 2448;
        </script>
        <img value='2' src='Albums/Test1/concealedjesspermit.jpg'>
        <script type='text/javascript'>
            picture_list[2] = 'Albums/Test1/concealedjesspermit.jpg';
            picture_x[2] = 325;
            picture_y[2] = 300;
        </script>
        Page 1
    </div>
</div>

</body>
</html>

这是我目前正在使用的 CSS。如果我删除 jQuery block 中的大小限制,div 不会调整大小。

{margin: 0; padding: 0;}
@import url(http://fonts.googleapis.com/css?family=Finger+Paint);
@import url(http://fonts.googleapis.com/css?family=Russo+One);
@font-face
{
    font-family: "DayPosterBlack";
    src: url('dayposterblack.tff');

}
body{
    background-color: #303030;
    background:#222 url(dark_wall.png);
}
#container {
    width: 1000px;
    height: 425px;
    overflow: hidden;
    margin: 25px auto;
    background:#303030;
}
.photobanner {
    height: 233px;
    width: 3550px;
    margin-bottom: 80px;
}
.first {
    -webkit-animation: bannermove 30s linear infinite;
    -moz-animation: bannermove 30s linear infinite;
    -o-animation: bannermove 30s liner infinite;
    animation: bannermove 30s linear infinite;
}
.photobanner img{
    -webkit-transition: all 0.5s ease;
    -moz-transition: all 0.5s ease;
    -ms-transition: all 0.5s ease;
    -o-transition: all 0.5s ease ;
    transition: all 0.5s ease;
}

.photobanner img:hover {
    -webkit-transform: scale(1.1);
    -moz-transform: scale(1.1);
    -o-transform: scale(1.1);
    -ms-transform: scale(1.1);
    transform: scale(1.1);
    cursor: pointer;

    -webkit-box-shadow: 0px 3px 5px rgba(0,0,0,0.2);
    -moz-box-shadow: 0px 3px 5px rgba(0,0,0,0.2);
    box-shadow: 0px 3px 5px rgba(0,0,0,0.2);
}
@keyframes "bannermove" {
    0%{
        margin-left: 0px;
    }
    100%{
        margin-left: -2125px;
    }
}

@-moz-keyframes "bannermove" {
    0%{
        margin-left: 0px;
    }
    100%{
        margin-left: -2125px;
    }
}

@-webkit-keyframes "bannermove" {
    0%{
        margin-left: 0px;
    }
    100%{
        margin-left: -2125px;
    }
}

@-ms-keyframes "bannermove" {
    0%{
        margin-left: 0px;
    }
    100%{
        margin-left: -2125px;
    }
}

@-o-keyframes "bannermove" {
    0%{
        margin-left: 0px;
    }
    100%{
        margin-left: -2125px;
    }
}
#nav {
 font-family: 'DayPosterBlack', normal;
 position: relative;
 background-color: black ;
 width: 1000px;
 height:25px;
 font-size:12px;
 color:#ffff33;
 margin: 0px auto;
 z-index: 10;

}

#nav ul {
 list-style-type: none;
 padding:0px;

}

#nav ul li {
 float: left;
 position: relative;
}

#nav ul li a {
 text-align: center;
 padding:55px;
 padding-top: 5px;
 padding-bottom: 5px;
 display:block;
 text-decoration:none;
 color:#ffff33;
}

#nav ul li ul {
 display: none
}

#nav ul li:hover ul {
 display: block;
 position: absolute;
}

#nav ul li:hover ul li a {
 display:block;
 background:#000000;
 color:#ffff33;
 width: 75px;
 text-align: center;
 border-bottom: 1px solid #f2f2f2;
 border-right: none;
}

#nav ul li:hover ul li a:hover {
 background:#303030;
 color:#fff;
 border-bottom: 1px solid #ffff33
}
#controlpanel {
 font-family: 'russo one', normal;
 position: relative;
 background: url('carbon_black.jpg');
 width: 1000px;
 height:25px;
 font-size:12px;
 color:#ffff33;
 margin: 0px auto;
 z-index: 0;

}

#controlpanel ul {
 list-style-type: none;
 padding:0px;

}

#controlpanel ul li {
 float: left;
 position: relative;
}

#controlpanel ul li a {
 text-align: center;
 padding:25px;
 padding-top: 5px;
 padding-bottom: 5px;
 display:block;
 text-decoration:none;
 color:#ffff33;
}

#controlpanel ul li ul {
 display: none
}

#controlpanel ul li:hover ul {
 display: block;
 position: absolute;
}

#controlpanel ul li:hover ul li a {
 display:block;
 background:#000000;
 color:#ffff33;
 width: 75px;
 text-align: center;
 border-bottom: 1px solid #f2f2f2;
 border-right: none;
}

#controlpanel ul li:hover ul li a:hover {
 background:#303030;
 color:#fff;
 border-bottom: 1px solid #ffff33
}
.album_container{
margin: 0px auto;
position: relative;
width: 1000px;
}
.album_box{
position: relative;
margin: 0px auto;
float: left;
text-align: center;
color: #ffff33;
width:325px;
height: 300px;
padding-right: 8px;
padding-bottom: 20px;
}
.album_box a{
font-family: 'russo one', sans serif ;
color: #ffff33;
text-align: center;
text-decoration: none ;

}
.album_box img{
position: relative;
margin: 0px;
display: block;
}
.album_box p{
font-family: 'russo one', sans serif;
color: #ffff33;
text-align: center;
position: absolute;
text-decoration: none;
margin: 0px;
z-index: 200px;
display: block;
background-color: #101010;
top: 280px;
}
.spacer {

position: relative;

width: 12px;
}
#gallery{
margin: 10px auto;
width: 1000px;



}
#thumpanel{
float: left;
width: 1000px;
border: 3px outset #101010;
background: url('dark_wall copydarker.png');

}


#thumbpanel img{
position: relative;
float: left;
padding: 4px;
}

#imageBackground{
position: fixed;
background: #303030;
opacity: 0.6 ;
width: 1500px;
height: 2000px;
top: 50%;
left: 50%;
margin: auto;
z-index: 20 ;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate( -50%, -50%);
transform: translate( -50%, -50%);

}
#largeImage{
position: fixed;

background: #202020 ;
top: 50%;
left: 50%;
margin: auto;
z-index: 25 ;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate( -50%, -50%);
transform: translate( -50%, -50%);

}
 #largeImage img{
position: absolute;
top: 0 ;
left: 0;
right: 0;
bottom: 0;
margin: auto ;

}
.return{
position: relative;
margin: 10px auto;
}
.back{
float: left;
position: relative;
margin-top: 240px;
margin-left: 20px;
background: #101010;
text-align: center;
color: #ffff33;
border: 2px outset #000000;
text-decoration: none;
width: 35px;
height: 35px;
vertical-align: middle;



}
.next{
float: right;
position: relative;
margin-top: 240px;
margin-right: -15px;
background: #101010;
text-align: center;
color: #ffff33;
border: 2px outset #000000;
text-decoration: none;
width: 35px;
height: 35px;
vertical-align: middle;
}
.close{
float: right;
position: relative;
margin-top: 0px;
margin-right: 0px;
background: #101010 ;
text-align: center;
color: #ffff33;
border: 2px outset #000000;
text-decoration: none;
width: 35px;
height: 35px;
}
#largeImage input [type='button']:hover{
background: #303030 ;

}
.data_container{
position: relative;
width: 1000px;
height: 1000px;
margin: 20px auto;

}
.data_entry{
position: relative ;
width: 1000px;
margin: 5px auto ;
}

.data_entry table{

margin: 0px auto;
font-size: 16px;
color: #ffff33 ;
}
.data_entry form{
background: url('dark_wall copydarker.png') ;
border: 3px outset #000000;
margin: 0px auto;
font-size: 16px;
color: #ffff33 ;

}
.success{
position: relative;
width: 1000px;
background: #ffff33 ;
margin: 5px auto ;
font-size: 20px;
}
.error{
position: relative;
width: 1000px;
background: #ff0000;
margin: 5px auto;
font-size: 20px;
}
.button{
margin: 5px auto ;
width: 180px;
display: block;
background: #101010;
text-align: center;
color: #ffff33;
border: 2px outset #000000;
text-decoration: none;
vertical-align: middle;
}
.button a:hover{
background: #303030 ;
}
.bottom_button{
position: relative;
bottom: 0 ;
margin: 5px auto ;
width: 180px;
display: block;
background: #101010;
text-align: center;
color: #ffff33;
border: 2px outset #000000;
text-decoration: none;
vertical-align: middle;
float: bottom ;
}
.bottom_button a:hover{
background: #303030 ;
}
.confirm{
margin: 5px auto ;
width: 180px;
display: block;
background: #101010;
text-align: center;
color: #ffff33;
border: 2px outset #000000;
text-decoration: none;
vertical-align: middle;
}
.confirm a:hover{
background: #303030;
}
.uploaded_photos{
position: relative;
width: 1000px;
height: 500px;
margin: 0px auto ;
background: url('dark_wall copydarker.png') ;
color: #ffff33 ;
text-align: center;
border: 2px outset #000000;
}
.uploaded_photos img{
width: 40% ;
height: 40%;
float: left;
margin: 10px;

}
.pageHeader{
text-align: center ;
color: #ffff33 ; 
font-size: 22px;
font-weight: bold ;
background-color: #000000;
font-family: 'russo one', sans serif;
width: 1000px;
height: 25px;
position: relative;
margin: 5px auto;
}

最佳答案

删除行:

$('#largImage').height(parseInt(picture_y[index]) + 20);
$('#largImage').width(parseInt(picture_x[index]) + 50);

并让#largeImage 自动调整大小以适合图像。您可以使用 CSS(最大宽度、最大高度)控制 #largeImage 的最大尺寸。

关于javascript - 根据图片大小设置div高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28518216/

相关文章:

javascript - 通过选择框更新图像时如何在 Canvas 中保留 z-index

javascript - Codeigniter Ajax无限滚动,带有过滤器重复数据

php - 如何在事件发生时提醒用户?

php - 使用文本框 mysql php 从 mysql 数据库中取出号码

php - Laravel 在关系对象上的位置

javascript - 如何仅当div的宽度超过一定高度时才显示按钮javascript?

JQuery 对象上的 JavaScript 替换似乎不起作用

php - 图片文件权限不起作用

javascript - 使用jquery将选定的字段放入textarea

JQuery 预览图像并允许使用按钮删除图像