如何使用 jquery 动态调整幻灯片标题?
我仍在学习 jquery 和 javascript,所以请耐心等待。我使用了 jquery 中的教程来使用 jquery 的循环函数创建幻灯片。
它很棒,允许我旋转包含图像和标题的各个 div。
$(document).ready(function() {
$('#gallery').cycle({
fx: 'scrollRight',
timeout: 5000,
speed: 500,
delay: -2000,
pager: '#pager',
next: '#next',
prev: '#prev'
});
$('#playControl').text('Play');
$('.credit imAgE (I had to change this because of rules for newbies').each(function (){
var widthSpacer = $(this).attr('width');
$('.credit p').css('width', widthSpacer + 10);
});
$('#playControl').toggle(
function() {
$('#gallery').cycle('resume');
$(this).text('Pause');
},
function() {
$('#gallery').cycle('pause');
$(this).text('Play');
});
}); // end ready()
</script>
<div style="width: 640px;">
<div id="contentWrap">
<div id="main">
<div id="controls">
<span id="prev" class="control">Previous</span>
<span id="pager"></span>
<span id="next" class="control">Next</span>
<span id="playControl" class="control">Pause</span>
</div>
<div id="gallery">
<div class="credit"><image tag 1>
<p>Pic one caption</p>
</div>
<div class="credit"><image tag n">
<p >Pic caption n</p>
</div>
问题在于标题宽度仍然是容器的宽度。图像跨越整个容器宽度是可以的,但如果图像很窄则不行。
我一直在尝试调整 div 中的
标签,使其宽度与图像相同,但它只为每个
设置一次宽度属性;
在所有“credit”类 div
中,而不是依次循环遍历每个类。
I'm still learning jquery and javascript so please bear with me. I've used a tutorial in jquery to create a slideshow using jquery's cycle function.
It's great and allows me to rotate individual divs containing an image and a caption.
$(document).ready(function() {
$('#gallery').cycle({
fx: 'scrollRight',
timeout: 5000,
speed: 500,
delay: -2000,
pager: '#pager',
next: '#next',
prev: '#prev'
});
$('#playControl').text('Play');
$('.credit imAgE (I had to change this because of rules for newbies').each(function (){
var widthSpacer = $(this).attr('width');
$('.credit p').css('width', widthSpacer + 10);
});
$('#playControl').toggle(
function() {
$('#gallery').cycle('resume');
$(this).text('Pause');
},
function() {
$('#gallery').cycle('pause');
$(this).text('Play');
});
}); // end ready()
</script>
<div style="width: 640px;">
<div id="contentWrap">
<div id="main">
<div id="controls">
<span id="prev" class="control">Previous</span>
<span id="pager"></span>
<span id="next" class="control">Next</span>
<span id="playControl" class="control">Pause</span>
</div>
<div id="gallery">
<div class="credit"><image tag 1>
<p>Pic one caption</p>
</div>
<div class="credit"><image tag n">
<p >Pic caption n</p>
</div>
The problem is that the caption width remains that of the container. It is okay with images that span the whole container width, but not if they are narrow.
I've been trying to tweak the <p>
tag within the div so that its width is the same as the image, but it only sets the width property once for every <p>
in all "credit" class div
s, rather than cycling through each in turn.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在 CSS 中为容器设置一个
min-width
,也许还可以设置width:auto
。希望有帮助。尝试一下你的 CSS,听起来你可以使用 CSS 一般性地修复这个问题,而不是使用 jQuery 为每张幻灯片单独修复。
You should give your container a set
min-width
in the CSS, and maybe set thewidth:auto
. Hope that helps.Play around with your CSS, it sounds like you might be able to fix this generically using CSS rather than individually for each slide using jQuery.