gpt4 book ai didi

javascript - 如何获取 'img' 标题属性并将其放入 h2 中?

转载 作者:行者123 更新时间:2023-11-28 21:09:02 25 4
gpt4 key购买 nike

我正在使用Create a Thumbnail with Fading Caption Using jQuery中描述的插件,但我希望我的标题是 h2 并从 img 属性中抓取文本?

最佳答案

按照该插件代码,制作如下所示的 HTML,而不是插件示例:

<div class="item">
<a href="link">
<img src="link img" title="your title" width="125" height="125"/>
<div class="caption">
<h2></h2>
</div>
</a>
</div>

然后这是您需要的更新后的 jQuery,并附有注释以进行解释:

 $(function() {
// let's set a variable as our img attr title
var title = $("img").attr('title');

var move = -15;

//zoom percentage, 1.2 =120%
var zoom = 1.2;

//On mouse over those thumbnail
$('.item').hover(function() {
//Grab title from title attr
$("h2").hide().append(title).fadeIn('slow');

//Set the width and height according to the zoom percentage
width = $('.item').width() * zoom;
height = $('.item').height() * zoom;

//Move and zoom the image
$(this).find('img').stop(false,true).animate({'width':width, 'height':height, 'top':move, 'left':move}, {duration:200});

//Display the caption
$(this).find('div.caption').stop(false,true).fadeIn(200);
},
function() {
//Remove title
$("h2").empty();

//Reset the image
$(this).find('img').stop(false,true).animate({'width':$('.item').width(), 'height':$('.item').height(), 'top':'0', 'left':'0'}, {duration:100});

//Hide the caption
$(this).find('div.caption').stop(false,true).fadeOut(200);
});
});

工作示例:http://jsfiddle.net/u2HxY/40/

关于javascript - 如何获取 'img' 标题属性并将其放入 h2 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9151896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com