检查元素是否被溢出隐藏:隐藏的JQuery / JavaScript

时间:2015-06-12 13:54:53

标签: javascript jquery html css

我有一个日历,其中包含每天的活动列表。目前,我每天最多显示3个事件,并允许用户切换以展开列表。

我用overflow:hidden和max-height:XXpx属性隐藏列表。我正在尝试检测当前隐藏在该列表中的事件。

我环顾四周,无法找到任何可以检测到此内容的内容

我试过了:

  Option Explicit

    Sub Send_Row_Or_Rows_Attachment_1()
    'Working in 2000-2013
    'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
        Dim OutApp As Object
        Dim OutMail As Object
        Dim rng As Range
        Dim Ash As Worksheet
        Dim Cws As Worksheet
        Dim Rcount As Long
        Dim Rnum As Long
        Dim FilterRange As Range
        Dim FieldNum As Integer
        Dim mailAddress As String
        Dim NewWB As Workbook
        Dim TempFilePath As String
        Dim TempFileName As String
        Dim FileExtStr As String
        Dim FileFormatNum As Long

        On Error GoTo cleanup
        Set OutApp = CreateObject("Outlook.Application")

        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With

        'Set filter sheet, you can also use Sheets("MySheet")
        Set Ash = ActiveSheet

        'Set filter range and filter column (column with names)
        Set FilterRange = Ash.Range("A5:H" & Ash.Rows.Count)
        FieldNum = 1    'Filter column = A because the filter range start in column A

        'Add a worksheet for the unique list and copy the unique list in A1
        Set Cws = Worksheets.Add
        FilterRange.Columns(FieldNum).AdvancedFilter _
                Action:=xlFilterCopy, _
                CopyToRange:=Cws.Range("A1"), _
                CriteriaRange:="", Unique:=True

        'Count of the unique values + the header cell
        Rcount = Application.WorksheetFunction.CountA(Cws.Columns(1))

        'If there are unique values start the loop
        If Rcount >= 2 Then
            For Rnum = 2 To Rcount

                'Look for the mail address in the MailInfo worksheet
                mailAddress = ""
                On Error Resume Next
                mailAddress = Application.WorksheetFunction. _
                    VLookup(Cws.Cells(Rnum, 1).Value, _
                              Worksheets("Mailinfo").Range("A1:B" & _
                                    Worksheets("Mailinfo").Rows.Count), 2, False)
                On Error GoTo 0

                If mailAddress <> "" Then

                    'Filter the FilterRange on the FieldNum column
                    FilterRange.AutoFilter Field:=FieldNum, _
                                           Criteria1:=Cws.Cells(Rnum, 1).Value

                    'Copy the visible data in a new workbook
                    With Ash.AutoFilter.Range
                        On Error Resume Next
                        Set rng = .SpecialCells(xlCellTypeVisible)
                        On Error GoTo 0
                    End With

                    Set NewWB = Workbooks.Add(xlWBATWorksheet)

                    rng.Copy
                    With NewWB.Sheets(1)
                        .Cells(1).PasteSpecial Paste:=8
                        .Cells(1).PasteSpecial Paste:=xlPasteValues
                        .Cells(1).PasteSpecial Paste:=xlPasteFormats
                        .Cells(1).Select
                        Application.CutCopyMode = False
                    End With

                    'Create a file name
                    TempFilePath = Environ$("temp") & "\"
                    TempFileName = "Your data of " & Ash.Parent.Name _
                                 & " " & Format(Now, "dd-mmm-yy h-mm-ss")

                    If Val(Application.Version) < 12 Then
                        'You use Excel 97-2003
                        FileExtStr = ".xls": FileFormatNum = -4143
                    Else
                        'You use Excel 2007-2013
                        FileExtStr = ".xlsx": FileFormatNum = 51
                    End If

                    'Save, Mail, Close and Delete the file
                    Set OutMail = OutApp.CreateItem(0)

                    With NewWB
                        .SaveAs TempFilePath & TempFileName _
                              & FileExtStr, FileFormat:=FileFormatNum
                        On Error Resume Next
                        With OutMail
                            .To = mailAddress
                            .Subject = "Test mail"
                            .Attachments.Add NewWB.FullName
                            .HTMLBody = RangetoHTML(rng)
                            .Display  'Or use Send
                        End With
                        On Error GoTo 0
                        .Close savechanges:=False
                    End With

                    Set OutMail = Nothing
                    Kill TempFilePath & TempFileName & FileExtStr
                End If

                'Close AutoFilter
                Ash.AutoFilterMode = False

            Next Rnum
        End If

      cleanup:
        Set OutApp = Nothing
        Application.DisplayAlerts = False
        Cws.Delete
        Application.DisplayAlerts = True

        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    Function RangetoHTML(rng As Range)
    ' Works in Excel 2000, Excel 2002, Excel 2003, Excel 2007, Excel 2010, Outlook 2000, Outlook 2002, Outlook 2003, Outlook 2007, and Outlook 2010.
        Dim fso As Object
        Dim ts As Object
        Dim TempFile As String
        Dim TempWB As Workbook

        TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

        ' Copy the range and create a workbook to receive the data.
        rng.Copy
        Set TempWB = Workbooks.Add(1)
        With TempWB.Sheets(1)
            .Cells(1).PasteSpecial Paste:=8
            .Cells(1).PasteSpecial xlPasteValues, , False, False
            .Cells(1).PasteSpecial xlPasteFormats, , False, False
            .Cells(1).Select
            Application.CutCopyMode = False
            On Error Resume Next
            .DrawingObjects.Visible = True
            .DrawingObjects.Delete
            On Error GoTo 0
        End With

        ' Publish the sheet to an .htm file.
        With TempWB.PublishObjects.Add( _
             SourceType:=xlSourceRange, _
             Filename:=TempFile, _
             Sheet:=TempWB.Sheets(1).Name, _
             Source:=TempWB.Sheets(1).UsedRange.Address, _
             HtmlType:=xlHtmlStatic)
            .Publish (True)
        End With

        ' Read all data from the .htm file into the RangetoHTML subroutine.
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
        RangetoHTML = ts.ReadAll
        ts.Close
        RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                              "align=left x:publishsource=")

        ' Close TempWB.
        TempWB.Close savechanges:=False

        ' Delete the htm file.
        Kill TempFile

        Set ts = Nothing
        Set fso = Nothing
        Set TempWB = Nothing
    End Function

以及if (element.offsetHeight < element.scrollHeight || element.offsetWidth < element.scrollWidth) { // element has overflow value } else { // element doesn't have overflow value } 和&amp; element.offsetHeight为列表中的任何元素返回相同的值。

2 个答案:

答案 0 :(得分:1)

scrollHeightscrollWidth是DOM属性,而不是jQuery。

$('div').each(function() {
     // get scroll measurements from DOM element
     var contentHeight = this.scrollHeight;
     var contentWidth = this.scrollWidth;
     // get the visible measurements from jQuery object
     var $this = $(this);
     var visibleHeight = $this.height();
     var visibleWidth = $this.width();

     if (visibleHeight < contentHeight
         || visibleWidth < contentWidth ) {
         // element has overflow value
     } else {
         // element doesn't have overflow value
     }
 })

答案 1 :(得分:-1)

您应该检查offsetHeightscrollHeightoffsetWidthscrollWidth

 $("ul li").each(function(){
     var element = $(this);
     if (element.height() < element.scrollHeight || element.width() < element.scrollWidth) {
         // element has overflow value
     } else {
         // element doesn't have overflow value
     }
 });