Javascript 水平div图像滚动动态宽度
我的水平图像滚动有问题。 某些解决方案存在许多威胁,但没有一种解决方案适合我:(Javascript 水平div图像滚动动态宽度,javascript,html,css,scroll,image-gallery,Javascript,Html,Css,Scroll,Image Gallery,我的水平图像滚动有问题。 某些解决方案存在许多威胁,但没有一种解决方案适合我:( <div id="scroll"> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <script src="http://lichtspielfotografie.com/wp-conten
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
以下是“我的图像库”的链接:
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
我使用的css:
.photos {
width:10000px;
height:100%;
position:absolute;
left:0;
margin-left:0;
margin-top: 0;
}
#scroll{
position:absolute;
width:100%;
height:100%;
overflow-x:scroll;
overflow-y:hidden;
margin-left:0;
left:0;
margin-top: -8,25%; }
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
以及html/php:
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
$(文档).ready(函数(){
$('html,body,*')。鼠标滚轮(函数(e,delta){
这个.scrollLeft-=(增量*20);
e、 预防默认值();
});
});
我希望您能帮助我动态调整照片的宽度。我对这个问题感到非常失望。您可以尝试选择所有图像,并对所有图像进行迭代,获得它们的宽度,然后将其添加到容器中。另外,请注意,如果元素(图像)之间有一些间距,您也应该添加这些间距
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
// get all images in an html collection
var imgs = document.getElementsByTagName('img');
// initialize sum variable for holding all the images width sum
var sum = 0;
// iterate the html collection
for(var i = 0; i < imgs.length; i++) {
// wait for images to load
imgs[i].onload(function() {
sum += imgs[i].width;
});
}
// add the width to the parent element
document.getElementsByClassName('photos')[0].style.width = sum;
//获取html集合中的所有图像
var imgs=document.getElementsByTagName('img');
//初始化sum变量以保存所有图像的宽度和
var总和=0;
//迭代html集合
对于(变量i=0;i
首先,更多地练习css样式和html。从照片之间删除
,并在照片之间留出空白。您可以执行以下操作:
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
.photos img {
margin: 0 2px;
}
然后,要计算图像的总宽度,需要使用javascript
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
您需要循环浏览图像并获得每个图像的宽度。如果我们假设您接受了我的建议,并且您已将
替换为css样式(边距),我们将使用。这将获得图像宽度及其填充和边框宽度。如果我们这样设置:。outerWidth(true)
它也将获得页边空白。这是我们需要的。我们将使用这些图像进行循环
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
我们开始:
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
var totalWidth = 0;
$('.photos > img').each(function() {
totalWidth += $(this).outerWidth(true);
});
现在我们只需要为图像容器设置新的宽度:
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
$('.photos').css('width', totalWidth + 'px');
如果您还有其他问题,请随时提问。祝您好运:)
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
编辑:
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
您必须在加载html时执行此操作。您的代码中已经有了这些功能。它是$(document).ready(function(){})代码>。在这里,您放置了希望在加载html后执行的所有javascript代码
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
因此,在本例中,您将执行以下操作:
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->
$(document).ready(function() {
// ... other js code here ...
var totalWidth = 0;
$('.photos > img').each(function() {
totalWidth += $(this).outerWidth(true);
});
// ... other js code here ...
});
您还可以将代码放入另一个js文件中,并将其加载到js库(如jQuery)下方的头部。非常感谢您的快速回答。我不熟悉stackoverflow和整个编程过程。你的想法听起来不错,但我不明白如何将这个javascriptcode片段实现到我的php文件中。我必须把它放在前面的部分吗?或者在特定部门之间非常感谢你。它对我起了作用,我学到了一些关于脚本的知识:)
<div id="scroll">
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://lichtspielfotografie.com/wp-content/themes/lichtspiel/js/jquery.mousewheel.js?ver=3.8.1"></script>
<script type="text/javascript">
$(document).ready(function() {
$('html, body, *').mousewheel(function(e, delta) {
this.scrollLeft -= (delta * 20);
e.preventDefault();
});
});
</script>
<div class="photos">
<?php global $post;
$src = '';
$breite='0px';
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID );
$attachments = get_posts($args); ?>
<?php
if ($attachments) {
foreach ( $attachments as $attachment ) {
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {
echo '<img src='.$src[0];'';
}
echo ' height="100%"/img> ';
}}
?>
</div>
</div>
</main><!-- #main -->
</div><!-- #primary -->