完善跑马灯效果——内容长时有横向滚动效果

之前的代码实现了跑马灯的纵向的滚动效果,每条内容长度固定,如果想实现纵向加横向的滚动效果,就是说完全显示内容的话,代码如下:
html代码:

<div class="shouHead clear">
    <div class="shouLeft">
        <span class="ywxian"></span><span class="ywfont">要闻通知</span>
    </div>
    <div class="shouRight">
        <ul class="slideTopbox" slide-scroll style="top:0px;">
            <li><a href="">AngularJS 通过新的属性和表达式扩展了 HTML。</a></li>
            <li><a href="">2016-8月AngularJS 可以构建一个单一页面应用程序(SPAs:Single Page Applications)。</a></li>
            <li><a href="">第三季度AngularJS 可以构建一个单一页面应用程序(SPAs:Single Page Applications)。</a></li>
            <li><a href="">关于AngularJS 可以构建一个单一页面应用程序(SPAs:Single Page Applications)。</a></li>
        </ul>
    </div>
</div>

css代码:

.shouHead {
    width: 100%;
    background: #FFFFFF;
    height: 50px;
}
.shouLeft {
    float: left;
    width: 2.04rem;
    line-height: 50px;
    color: #4371B3;
    font-size: .32rem;
    background-size: 80px;
}
.ywxian {
    display: inline-block;
    width: .08rem;
    height: .24rem;
    background: #34d1f2;
    margin-left: 8px;
}
.ywfont {
    margin-left: 8px;
}
.shouRight {
    float: left;
    width: 4.36rem;
    height: 30px;
    text-align: left;
    line-height: 1rem;
    font-size: 16px;
    color: #333;
    margin-top: 10px;
    overflow: hidden;
    position: relative;
}
.clear:after {
    content: "";
    clear: both;
    visibility: hidden;
    display: block;
    height: 0;
}

js代码:

angular.module('starter.workspaceServices', [], function($compileProvider) {})
.directive('slideScroll', function($window, $timeout) {
    return {
        restrict: 'AE',
        link: function(scope, element, attr) {
            var itsWatch = scope.$watch("its", function(newvalue, oldvalue) {
                if(oldvalue != newvalue) {
                    itsWatch();
                    var i = 0;
                    var length = element[0].children.length;
                    var widthwindow = $window.innerWidth - 110;  //根据自己定义的宽度进行减少
                    var firstwidth = element[0].children[0].children[0].offsetWidth;

                    setInterval(function() {
                        if(i == length) {
                            i = 0;
                            element[0].style.top  = "0px";
                        }

                        var topscorll = -(i * 30);
                        var widthself = element[0].children[i].children[0].offsetWidth;
                        if(widthself > widthwindow) {
                    leftGundong(element[0].children[i].children[0])
                        }

                        feeltoTop(topscorll);
                        i++;
                    }, 3000)
                    function feeltoTop(topscorll){                      
                        var buchang = -10;
                        var feelTimer = setInterval(function(){
                            element[0].style.top = parseInt(element[0].style.top) + buchang + "px";console.log(element[0].style.top)
                            if(parseInt(element[0].style.top) <= topscorll){
                                element[0].style.top = topscorll + "px";
                                window.clearInterval(feelTimer);
                            }
                        },100);

                    }
                    function leftGundong(target) {
                        var tWidth = target.offsetWidth;
                        target.style.left = "0px";
                        target.style.position = "absolute";
                        //target.style.marginLeft = "0px";
                        var juli = -3; //步长;
                        var leftAll = widthwindow - parseInt(tWidth); //需要移动的距离
                        setTimeout(function() {
                            var leftTimer = setInterval(function() {
                                var tLeft = parseInt(target.style.left);
                                target.style.left = tLeft + juli + "px";
                                if(tLeft <= leftAll) {
                                    window.clearInterval(leftTimer);
                                    setTimeout(function(){
                                        //target.style.left = "0px";
                                    },1000)
                                }
                            }, 100)
                        }, 1000)

                    }
                }
            })

        }
    }
})

效果为:
这里写图片描述

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值