【问题标题】:Animate a paragraph along with rotating images动画段落以及旋转图像
【发布时间】:2011-03-15 10:50:51
【问题描述】:

基本上有 4 张图片会滚动到页面外。现在,当用户单击链接时,该链接会滚动。但是我想添加一个额外的动画,其中一个段落(“.image_text p”)也出现在页面上,然后当单击另一个链接时,该文本与图像的其余部分一起滚动并滚动。除了段落部分的动画外,一切正常。

问题:

  1. 文本全部挤在一起(有四个段落,它们相互重叠)

  2. 当我点击时——它甚至没有时间让它消失——而敌人在它消失之前就从页面上飞走了。

  3. 第一次加载页面时,第一段甚至根本不出现 - 单击按钮时,所有段落都会出现。它应该是页面加载时出现的第一段,然后单击然后下一段显示为第一次滚动图像。

标记和 css 是正确的。我认为问题出在这个javascript的某个地方:

      $(function() {
        $('#slideshow').crossSlide({
          sleep: 2,
          fade: 1
        }, [
          { src: '../images/img1.png' },
          { src: '../images/img2.png' },
          { src: '../images/img3.png' },
          { src: '../images/img4.png' }
        ])

                $(".paging").show();
                $(".paging a:first").addClass("active");
                $(".image_text p:first").addClass("active");


                //Get size of the image, how many images there are, then determin the size of the image reel.
                var imageWidth = $(".window").width();
                var imageSum = $(".image_reel img").size();
                var imageReelWidth = imageWidth * imageSum;

                //Adjust the image reel to its new size
                $(".image_reel").css({'width' : imageReelWidth});

                //Paging  and Slider Function
                rotate = function(){
                    $activep = $('.image_text p:first');


                    var triggerID = $active.attr("rel") - 1; //Get number of times to slide
                    var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

                    $(".paging a").removeClass('active'); //Remove all active class
                    $(".image_text p").removeClass('active'); //Remove all active class

                    $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
                    $activep.addClass('active');

                    //Slider Animation
                    $(".image_reel").animate({
                        left: -image_reelPosition
                    }, 500 );

                    $(".image_text p").animate({
                        opacity: .99,
                        left: -image_reelPosition
                    }, 500 );

                }; 

                //Rotation  and Timing Event
                rotateSwitch = function(){
                    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
                        $active = $('.paging a.active').next(); //Move to the next paging
                        $activep = $('.image_text p').next(); //Move to the next paging
                        if ( $active.length === 0) { //If paging reaches the end...
                            $active = $('.paging a:first'); //go back to first
                        }
                        rotate(); //Trigger the paging and slider function
                    }, 7000); //Timer speed in milliseconds (7 seconds)
                };

                rotateSwitch(); //Run function on launch

                //On Hover
                $(".image_reel a").hover(function() {
                    clearInterval(play); //Stop the rotation
                }, function() {
                    rotateSwitch(); //Resume rotation timer
                }); 

                //On Click
                $(".paging a").click(function() {
                    $active = $(this); //Activate the clicked paging
                    //Reset Timer
                    clearInterval(play); //Stop the rotation
                    rotate(); //Trigger rotation immediately
                    rotateSwitch(); // Resume rotation timer
                    return false; //Prevent browser jump to link anchor
                });
      });

段落的CSS:

.image_text p {
text-align: right;
width: 500px;
opacity: 0;
position: absolute;
top: 0;
left: 0;
 }

可以看出,不显示文本的原因是因为不透明度从 0 开始。但这是故意的,因为它应该淡入。但是,它似乎不会淡入,而只会变成单击链接时可见,到那时它会滚动到页面外。

【问题讨论】:

  • 如果我是你,我会保持原样(或改写它),然后在解决方案中添加答案,然后在服务器允许时立即接受我自己的答案.
  • 顺便说一句,您可以回答自己的问题。事实上,有一个徽章可以用至少 3(?) 票来回答你自己的问题。是的,它被称为自学习者:stackoverflow.com/badges/14/self-learner

标签: javascript jquery animation rotation opacity


【解决方案1】:

解决方案是通过容器的宽度指定 p 标签和多个标签的倍数,然后使用 css 声明 overflow:hidden 指定容器,它隐藏除了当前的所有 p 标签。此外,p 标签必须向左浮动才能使它们内联:

.window {
    margin-top: 20px;
    height:286px;
    width: 655px;
    overflow: hidden; 
    position: relative;
} 

.image_text  {
    position: absolute;
    top: 0;
    left: 0;
    opacity: 0;
}

.image_text p {
    float: left;
    margin-left: 10em;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2011-04-15
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多