Javascript 如何将文本设置为右侧动画

Javascript 如何将文本设置为右侧动画,javascript,jquery,html,css,Javascript,Jquery,Html,Css,小提琴: JQuery: $(function () { $(".content-box").hover(function () { $(this).children("div:first-child").find(".hdrText").animate({ 'marginLeft': '+=100%' }, 2000); }, function () { $(this).children("div:first-child").find(".hdr

小提琴:

JQuery:

$(function () {
    $(".content-box").hover(function () {
        $(this).children("div:first-child").find(".hdrText").animate({ 'marginLeft': '+=100%' }, 2000);
    }, function () {
        $(this).children("div:first-child").find(".hdrText").animate({ 'marginLeft': '-=100%' }, 2000);
    });
});

如何修改脚本,以便在悬停时,文本向右移动,直到到达右侧黑色边框;在悬停后,文本返回到原始位置。

您只需使用CSS即可完成此操作,无需使用jQuery代码。当
.content box
悬停时,它基本上是通过更改
.hdrText
来工作的

演示:

.content框{
位置:相对;/*用于儿童工作时的绝对位置*/
}
.hdrText{
位置:绝对位置;
顶部:4px;
左:44px;
右:100%;/*允许我们设置右偏移的动画*/
文本对齐:右;/*确保文本不会溢出*/
过渡:2s;
}
.content框:hover.hdrText{
右:10px;/*将右偏移设置为10px(与边缘的间隙)
}

公告
Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。信息技术
它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布而流行,最近随着desktop的发布而流行
发布软件,如Aldus PageMaker,包括Lorem Ipsum版本。

您只需使用CSS即可完成此操作,无需使用jQuery代码。当
.content box
悬停时,它基本上是通过更改
.hdrText
来工作的

演示:

.content框{
位置:相对;/*用于儿童工作时的绝对位置*/
}
.hdrText{
位置:绝对位置;
顶部:4px;
左:44px;
右:100%;/*允许我们设置右偏移的动画*/
文本对齐:右;/*确保文本不会溢出*/
过渡:2s;
}
.content框:hover.hdrText{
右:10px;/*将右偏移设置为10px(与边缘的间隙)
}

公告
Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一位不知名的印刷商拿起一个打印工具,将其拼凑成一本打印样本书。信息技术
它不仅存活了五个世纪,而且还跨越到电子排版,基本上保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布而流行,最近随着desktop的发布而流行
发布软件,如Aldus PageMaker,包括Lorem Ipsum版本。

首先,您需要在右侧添加边距,以抵消添加到左侧的边距。这将给人一种文本移动而不调整自身大小或在页面上移动的印象。您将需要相等数量的负数:

{'marginLeft': '+=100%', 'marginRight': '-=100%'}
我还注意到,动画在快速悬停时会变得跳跃。您可以查看mouseenter和mouseleave中的内容,并在设置动画之前添加一个stop(),以取消上一个动画:


首先,您需要在右侧添加边距,以抵消添加到左侧的边距。这将给人一种文本移动而不调整自身大小或在页面上移动的印象。您将需要相等数量的负数:

{'marginLeft': '+=100%', 'marginRight': '-=100%'}
我还注意到,动画在快速悬停时会变得跳跃。您可以查看mouseenter和mouseleave中的内容,并在设置动画之前添加一个stop(),以取消上一个动画:


如果您想要纯jQuery支持,您可以这样做,您只需要计算出要加和减的确切比例,并考虑到所有其他元素

$(function() {
    // Load all needed elements
    var content = $(".content-box");
    var contentTitle = $(".content-box-title");
    var text = content.children("div:first-child").find(".hdrText");
    var theimage = $('#theimage');
    var currentAnimate = null;
    content.hover(function() {
        // Get titles total width
        var contentWidth = contentTitle.width();
        // Get the texts total width
        var textWidth = text.width();
        // Get the images total width
        var theimageWidth = theimage.width();
        // Get the padding on the image
        var imageParentPadding = theimage.parent().css('padding-right');
        // Add text, image and padding + 5 to accommodate changing screen size together so we can subtract that from content width
        var subtractWidth = textWidth + theimageWidth + parseInt(imageParentPadding) + 5;
        // Save value to move back to same position in case screen size changes between animations
        currentAnimate = contentWidth - subtractWidth;
        // Animate margin left by the total content width minus the width of all the other elements and paddings
        text.animate({
            'marginLeft': '+=' + currentAnimate
        }, 2000);
    }, function () {
        text.animate({
            'marginLeft': '-=' + currentAnimate
        }, 2000);
    });
});

Fiddle:

如果您想要纯jQuery支持,您可以做到这一点,您只需要在考虑所有其他元素的情况下计算要加和减的确切比例

$(function() {
    // Load all needed elements
    var content = $(".content-box");
    var contentTitle = $(".content-box-title");
    var text = content.children("div:first-child").find(".hdrText");
    var theimage = $('#theimage');
    var currentAnimate = null;
    content.hover(function() {
        // Get titles total width
        var contentWidth = contentTitle.width();
        // Get the texts total width
        var textWidth = text.width();
        // Get the images total width
        var theimageWidth = theimage.width();
        // Get the padding on the image
        var imageParentPadding = theimage.parent().css('padding-right');
        // Add text, image and padding + 5 to accommodate changing screen size together so we can subtract that from content width
        var subtractWidth = textWidth + theimageWidth + parseInt(imageParentPadding) + 5;
        // Save value to move back to same position in case screen size changes between animations
        currentAnimate = contentWidth - subtractWidth;
        // Animate margin left by the total content width minus the width of all the other elements and paddings
        text.animate({
            'marginLeft': '+=' + currentAnimate
        }, 2000);
    }, function () {
        text.animate({
            'marginLeft': '-=' + currentAnimate
        }, 2000);
    });
});

Fiddle:

如果内容不是太动态,如图像中的大小是固定的,则可以使用以下内容:

$(function () {
    var hdr =$(".content-box").hover(function () {      
    hdr.stop().animate( {left: $(this).parent().width() - hdr.width() - startoffset } , 2000);
    }, function () {
    hdr.stop().animate({left:0}, 2000);
    }).find("div:first-child>.hdrText").css('position', 'relative');
  var startoffset = hdr.offset().left;
});

左动画需要相对位置,在这里用jquery设置,但也可以在css中设置。
.stop()是一个额外添加项,用于确保上一个动画停止。e、 g.悬停结束时,左侧的动画仍在执行。

如果内容不是太动态,如图像中的大小是固定的,则可以使用以下方法:

$(function () {
    var hdr =$(".content-box").hover(function () {      
    hdr.stop().animate( {left: $(this).parent().width() - hdr.width() - startoffset } , 2000);
    }, function () {
    hdr.stop().animate({left:0}, 2000);
    }).find("div:first-child>.hdrText").css('position', 'relative');
  var startoffset = hdr.offset().left;
});

左动画需要相对位置,在这里用jquery设置,但也可以在css中设置。
.stop()是一个额外添加项,用于确保上一个动画停止。e、 g.悬停结束时,左侧的动画仍在执行。

您不需要javascript,我正在使用IE9,不幸的是,这是目标。您不需要javascript,我正在使用IE9,不幸的是,这是目标。但它只是继续前进,并没有回到mouseout上的原始位置。但是它一直在运行,没有回到mouseout上的原始位置。我想要Jquery版本,因为它可以与IE9一起工作。jsfiddle.net/6avyo8zf/1但它只是继续运行,并没有回到mouseout上的原始位置。@SiKni8使用jQuery解决方案进行了更新,请告诉我这是否适用于您。我希望jQuery版本能够与IE9一起使用。jsfiddle.net/6avyo8zf/1但它只是继续运行,并没有回到mouseout上的原始位置。@SiKni8使用jQuery解决方案进行了更新,请告诉我这是否对您有效。谢谢。我刚刚测试了小提琴,可能正是我需要的。我将在我的项目中进行测试并更新。只有一个问题,如果我有多个相同的类,我如何确保它只针对我悬停过的那个类?一种方法是附加悬停事件sepa